# leetcode刷题，总结，记录，备忘 75

leetcode75

### 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) {
vector<int> temp;
vector<int>::iterator it = nums.begin();
while ((it = find(it, nums.end(), 0)) != nums.end())
{
temp.push_back(*it);
it++;
}
it = nums.begin();
while ((it = find(it, nums.end(), 1)) != nums.end())
{
temp.push_back(*it);
it++;
}
it = nums.begin();
while ((it = find(it, nums.end(), 2)) != nums.end())
{
temp.push_back(*it);
it++;
}
nums = temp;
}
};

class Solution {
public:
void sortColors(vector<int>& nums) {
if (nums.size() <= 1)
return;
int s = -1, e = nums.size();
int it = 0, temp;
while (it < nums.size())
{
if (nums[it] == 0)
{
if (it > s)
{
temp = nums[it];
nums[it] = nums[++s];
nums[s] = temp;
}
else it++;
}else if  (nums[it] == 2)
{
if (it < e)
{
temp = nums[it];
nums[it] = nums[--e];
nums[e] = temp;
}
else
it++;
}
else
it++;
}

}
};

#### leetcode刷题，总结，记录，备忘 201

2016-04-24 23:45:59

#### leetcode刷题，总结，记录，备忘300

2015-11-08 20:04:14

#### LeetCode 75 解题与优化

2017-03-16 17:34:21

#### Leetcode 75 python

2018-02-06 17:48:28

#### leetcode刷题， 总结，记录，备忘145

2015-07-09 15:27:07

#### leetcode刷题，总结，记录，备忘 343

2016-05-08 21:03:16

#### leetcode 75 分类颜色

2018-05-09 19:30:29

#### leetcode刷题，总结，记录，备忘 367

2016-07-26 23:30:41

#### LeetCode（75） Sort Colors

2015-09-17 20:00:39

#### leetcode刷题，总结，记录，备忘89

2015-07-20 23:02:20