Skip to content

Commit

Permalink
Create: 0075-sort-colors.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
loczek committed Dec 28, 2022
1 parent ee7a3bc commit 2cc0e43
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions typescript/0075-sort-colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
Do not return anything, modify nums in-place instead.
*/
function sortColors(nums: number[]): void {
let l = 0;
let r = nums.length - 1;
let i = 0;

function swap(a: number, b: number) {
let temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}

while (i <= r) {
if (nums[i] == 0) {
swap(l, i);
l += 1;
} else if (nums[i] == 2) {
swap(r, i);
r -= 1;
i -= 1;
}
i += 1;
}
}

0 comments on commit 2cc0e43

Please sign in to comment.