Skip to content

Commit 4959aae

Browse files
authored
Update 75-sort-colors.js
1 parent 5fad49a commit 4959aae

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

75-sort-colors.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
*/
55

66
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++;
7+
let i = 0
8+
let j = 0
9+
for(let k = 0; k < nums.length; k++) {
10+
let v = nums[k]
11+
nums[k] = 2
12+
if(v < 2) {
13+
nums[j] = 1
14+
j += 1
15+
}
16+
17+
if(v === 0) {
18+
nums[i] = 0
19+
i += 1
2020
}
21-
val+=1;
2221
}
2322

2423
return nums;

0 commit comments

Comments
 (0)