LeetCode 75.
Tags: LeetCode
class Solution {
public void sortColors(int[] nums) {
//[2,0,2,1,1,0] => [0,0,1,1,2,2]
int[] cnt = new int[3];
for (int i=0; i<nums.length; i++) {
cnt[nums[i]]++;
}
int indA = 0, indB = 0;
while (indA < 3) {
while (cnt[indA]-- > 0) {
nums[indB++] = indA;
}
indA++;
}
}
}
Check out the description of this problem at LC 75.