Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library’s sort function for this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Solution { public: void sortColors(vector<int>& nums) { int cnt0 = 0, cnt1 = 0, cnt2 = 0; for(int i = 0; i < nums.size(); i++) { if(nums[i] == 0) { cnt0++; } else if (nums[i] == 1) { cnt1++; } else { cnt2++; } } for(int i = 0; i < cnt0; i++) { nums[i] = 0; } for(int i = cnt0; i < cnt0 + cnt1; i++) { nums[i] = 1; } for(int i = cnt1 + cnt0; i < nums.size(); i++) { nums[i] = 2; } } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼