Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1621 from loczek/0075-sort-colors
Browse files Browse the repository at this point in the history
Create: 0075-sort-colors.ts
  • Loading branch information
Ahmad-A0 authored Dec 29, 2022
2 parents 9b78cfc + 2cc0e43 commit 2dc60bd
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 2dc60bd

Please sign in to comment.