75 - Sort Colors
2016-03-16 10:39
295 查看
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.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
Subscribe to see which companies asked this question
思路分析:
This is a relative easy problem.
The goal is to rearrange the array, where 0s are in the 1st part of the array, 1s in the middle, and 2s are in the last. So, we can just scan the whole array, when meet 0, put it in the front, when meet 2, put it in the last, and leave 1 alone thus in the
middle. 0 and 2's positions are stored and modified each time a swap is performed.
Details see the code.
代码如下:
class Solution {
public:
void swap(vector<int>& nums, int i, int j)
{
int tmp = nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
void sortColors(vector<int>& nums)
{
int red = 0;
int blue = nums.size() - 1;
while (nums[red]==0){red++;}
while (nums[blue]==2){blue--;}
int i=red;
while (i<=blue){
if (nums[i]==0 && i>red) {swap(nums,i,red);red++;continue;}
if (nums[i]==2) {swap(nums,i,blue);blue--;continue;}
i++;
}
}
};
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.
click to show follow up.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
Subscribe to see which companies asked this question
思路分析:
This is a relative easy problem.
The goal is to rearrange the array, where 0s are in the 1st part of the array, 1s in the middle, and 2s are in the last. So, we can just scan the whole array, when meet 0, put it in the front, when meet 2, put it in the last, and leave 1 alone thus in the
middle. 0 and 2's positions are stored and modified each time a swap is performed.
Details see the code.
代码如下:
class Solution {
public:
void swap(vector<int>& nums, int i, int j)
{
int tmp = nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
void sortColors(vector<int>& nums)
{
int red = 0;
int blue = nums.size() - 1;
while (nums[red]==0){red++;}
while (nums[blue]==2){blue--;}
int i=red;
while (i<=blue){
if (nums[i]==0 && i>red) {swap(nums,i,red);red++;continue;}
if (nums[i]==2) {swap(nums,i,blue);blue--;continue;}
i++;
}
}
};
相关文章推荐
- Activiti 简易教程一 version5.10
- 来自安卓菜鸟的独白
- 使用Delphi调用条形码控件BarTender打印标签
- 电脑公司最新GHOST WIN7系统32,64位优化精简版下载
- GitHub衍生工具hub、Travis CI、Converalls、Jenkins
- 北京Uber优步司机奖励政策(3月16日)
- listener 实现及配置
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
- 文件缓存
- Angular自定义指令(进阶)
- 阅读《Android 从入门到精通》(31)——Intent
- java客户端Ip获取
- ccnu_2015级训练赛(1)题目解答
- java基础01
- java实例化代码------------------监听事件
- MemCache超详细解读
- 滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(3月16日)
- 编写一个能自动生成小学四则运算题目的程序
- mysql2redis
- 0316复利计算