Skip to content

Commit 3407b5d

Browse files
committed
Add solution #75
1 parent 5501b98 commit 3407b5d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
70|[Climbing Stairs](./0070-climbing-stairs.js)|Easy|
7575
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
7676
74|[Search a 2D Matrix](./0074-search-a-2d-matrix.js)|Medium|
77+
75|[Sort Colors](./0075-sort-colors.js)|Medium|
7778
77|[Combinations](./0077-combinations.js)|Medium|
7879
78|[Subsets](./0078-subsets.js)|Medium|
7980
80|[Remove Duplicates from Sorted Array II](./0080-remove-duplicates-from-sorted-array-ii.js)|Medium|

solutions/0075-sort-colors.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 75. Sort Colors
3+
* https://leetcode.com/problems/sort-colors/
4+
* Difficulty: Medium
5+
*
6+
* Given an array nums with n objects colored red, white, or blue, sort them in-place so
7+
* that objects of the same color are adjacent, with the colors in the order red, white,
8+
* and blue.
9+
*
10+
* We will use the integers 0, 1, and 2 to represent the color red, white, and blue,
11+
* respectively.
12+
*
13+
* You must solve this problem without using the library's sort function.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {void} Do not return anything, modify nums in-place instead.
19+
*/
20+
var sortColors = function(nums) {
21+
const count = nums.reduce((a, n) => ++a[n] && a, [0, 0, 0]);
22+
for (let i = 0, j = 0; i < count.length; i++) {
23+
for (let k = 0; k < count[i]; k++) {
24+
nums[j++] = i;
25+
}
26+
}
27+
return nums;
28+
};

0 commit comments

Comments
 (0)