Skip to content

Commit 5fad49a

Browse files
authored
Create 75-sort-colors.js
1 parent 58fd73c commit 5fad49a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

75-sort-colors.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
6+
const sortColors = function(nums) {
7+
const counts = {};
8+
for (const n of nums) {
9+
if (!counts[n]) counts[n] = 0;
10+
counts[n]++;
11+
}
12+
13+
let val = 0;
14+
for (let i = 0; i < nums.length;) {
15+
let count = counts[val];
16+
while (count > 0) {
17+
nums[i] = val;
18+
count--;
19+
i++;
20+
}
21+
val+=1;
22+
}
23+
24+
return nums;
25+
};

0 commit comments

Comments
 (0)