给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红、白、蓝的顺序进行排序。
我们可以使用整数 0,1 和 2 分别代表红,白,蓝。
注意事项
不能使用代码库中的排序函数来解决这个问题。
排序需要在原数组中进行。
样例
给你数组 [1, 0, 1, 2], 需要将该数组原地排序为 [0, 1, 1, 2]。
挑战
一个相当直接的解决方案是使用计数排序扫描2遍的算法。
首先,迭代数组计算 0,1,2 出现的次数,然后依次用 0,1,2 出现的次数去覆盖数组。
你否能想出一个仅使用常数级额外空间复杂度且只扫描遍历一遍数组的算法?
#ifndef C148_H
#define C148_H
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
/**
* @param nums: A list of integer which is 0, 1 or 2
* @return: nothing
*/
void sortColors(vector<int> &nums) {
// write your code here
int len = nums.size();
if (len <= 0)
return;
int i = 0, j = len - 1;
int k = 0;
while (k < len&&k <= j)
{
if (nums[k] == 0)
{
if (k == i)
++k;
else
swap(nums[i], nums[k]);
++i;
}
else if (nums[k] == 2)
{
swap(nums[k], nums[j]);
--j;
}
else
++k;
}
}
};
#endif