75. Sort Colors
2018-03-07 18:57
316 查看
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.class Solution {
public:
void sortColors(vector<int>& nums) {
int zero = -1;
int two = nums.size();
for(int i = 0; i < two; )
{
if(nums[i] == 0){
zero++;
swap(nums[zero],nums[i]);
i++;
}
else if(nums[i] == 2){
two--;
swap(nums[two],nums[i]);
}
else if(nums[i] == 1){
i++;
}
}
}
};
You are not suppose to use the library's sort function for this problem.class Solution {
public:
void sortColors(vector<int>& nums) {
int zero = -1;
int two = nums.size();
for(int i = 0; i < two; )
{
if(nums[i] == 0){
zero++;
swap(nums[zero],nums[i]);
i++;
}
else if(nums[i] == 2){
two--;
swap(nums[two],nums[i]);
}
else if(nums[i] == 1){
i++;
}
}
}
};
相关文章推荐
- 75. Sort Colors
- leetcode 75. Sort Colors
- LeetCode||75. Sort Colors
- 75. Sort Colors
- Leetcode 75. Sort Colors (Medium) (cpp)
- [置顶] 75. Sort Colors--荷兰三色国旗问题
- leetcode_middle_62_75. Sort Colors
- [LeetCode] 75. Sort Colors java
- LeetCode 75. Sort Colors
- 75. Sort Colors(unsolved)
- Sort Colors - LeetCode 75
- Leetcode 75. Sort Colors
- [leetCode 75] Sort Colors
- [75]Sort Colors
- leetcode 75. Sort Colors
- leetcode: 75. Sort Colors
- LeetCode OJ 75. Sort Colors
- 【一天一道LeetCode】#75. Sort Colors
- 280. Wiggle Sort && 324. Wiggle Sort II && 75. Sort Colors
- leetcode 75. Sort Colors