###### 75. Sort Colors

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 i = -1;
int j = nums.size();
for (int k = 0; k < j; k++){
if (nums[k] == 0){
i++;
swap(nums[i], nums[k]);
}
else if (nums[k] == 2){
j--;
swap(nums[j], nums[k]);
k--;
}
else{
//do nothing
}
}
}
};

class Solution {
public:
void sortColors(vector<int>& nums) {
int i = -1, j = nums.size();
int k = 0;
while (k < j){
switch (nums[k]){
case 0:
i++;
swap(nums[i], nums[k]);
break;
case 1:
break;
case 2:
j--;
swap(nums[k], nums[j]);
break;
}
k++;
}
return;
}
};

#### LeetCode（75） Sort Colors

2015-09-17 20:00:39

#### leetCode 75.Sort Colors (颜色排序) 解题思路和方法

2015-07-19 16:38:00

#### [C语言][LeetCode][75]Sort Colors

2016-12-12 16:13:08

#### 【LeetCode】Sort Colors 解题报告

2014-10-13 20:51:44

#### 【LeetCode-面试算法经典-Java实现】【075-Sort Colors （颜色排序）】

2015-07-30 06:36:18

#### LeetCode(75)Sort Colors (荷兰三色旗问题 Dutch National Flag)

2014-02-10 03:42:16

#### Leetcode_75_Sort Colors

2015-01-30 07:32:32

#### Leetcode75 Sort Colors

2015-10-18 20:42:20

#### 75. Sort Colors（第三周）

2017-09-24 18:00:02

#### [LeetCode] 75. Sort Colors

2016-08-02 14:21:54

75. Sort Colors