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.

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?

思路: 使用两个指针,分别指向最后一个红色的后一个位置,以及第一个蓝色蓝色的前一个位置。如果当前扫描到的元素是红色,那么与第一个指针互换,若为蓝色,与蓝色头部指针互换,若为白色,不做任何操作。

class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int redTail = ;
int blueHead = n-;
int tmp; for(int i = ; i <= blueHead;){
if(nums[i]==){//当前是红色
if(nums[redTail]==){//红色尾指针指向红色(说明没有白色)
redTail++;
i++; //不处理当前元素
continue;
} tmp = nums[redTail];
nums[redTail] = nums[i];
redTail++;
if(tmp==){ //红色尾指针指向白色
nums[i] = tmp; //交换红色和白色
i++;
}
else{ //红色尾指针指向蓝色(说明目前没有白色)
nums[i] = nums[blueHead]; //把blueHead处的元素放到i
nums[blueHead]=tmp; //把蓝色放到blueHead处
blueHead--;
}
}
else if(nums[i]==){//如果当前是白色,不处理当前元素
i++;
}
else{ //如果当前是蓝色
tmp = nums[blueHead];
nums[blueHead] = nums[i];//把蓝色放到blueHead处
nums[i] = tmp;//把blueHead处的元素放到i
blueHead--;
}
}
}
};

75. Sort Colors (Array)的更多相关文章

  1. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  3. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  5. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  6. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. Leetcode 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. LeetCode OJ 75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. 使用distillery 实现版本的动态升级&& 动态降级

    备注: distillery  使用很棒的elixir 打包构建工具,下面演示的是升级以及降级   1. 参考项目 https://github.com/rongfengliang/phoenix-r ...

  2. mui 修改下拉刷新提示文字的显示位置

    第一种: .mui-bar-nav~.mui-content .mui-pull-top-pocket { top: 126px !important; } 第二种: .mui-pull-top-po ...

  3. winform 多线程中ShowDialog()无效的解决办法

    在使用Winform开发的过程,不可避免的要使用多线程,其中会在多线程中会调用窗口的ShowDialog方法,但实际上并不是模式窗口. private void Form1_Load(object s ...

  4. 【转】VC 多线程中控制界面控件的几种方法

    原文网址:https://software.intel.com/zh-cn/blogs/2010/11/30/vc-3 为了保证界面的用户体验经常要把数据处理等放到子线程中进行,然后把结果更新到主界面 ...

  5. Spring整合JavaMail

    1.添加jar包 #此处省略spring基础相关jar包描述,以下是发送邮件相关jar包 <dependency> <groupId>org.springframework&l ...

  6. ubuntu下eclipse安装maven插件

    ubuntu科输入如下指令安装eclipse:sudo apt-get install eclipse ubuntu下安装maven插件打开Eclipse点击Help -> Install Ne ...

  7. .NET基础复习一

    . ] ; 等号左边开辟了一个小的栈的空间.等号右边在堆空间开辟了5个空间,会将堆里开辟的第一个空间给地址赋值给栈里的空间 ,]; productList[,] ="1号"; pr ...

  8. sourceTree 添加 ssh key 方法【转】

    1.使用 git 客户的生成公私钥:id_rsa.id_rsa.pub 1.1设置Git的user name和email: $ git config --global user.name " ...

  9. CStdioFile的Writestring无法写入中文的问题

    解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题 2009-12-01 23:11 以下代码文件以CStdioFile向无法向文本中写入中文(用notepad. ...

  10. GOF23设计模式之适配器模式(Adapter)

    一.适配器模式概述 将一个类的接口转换成客户可用的另外一个接口. 将原本不兼容不能在一起工作的类添加适配处理类,使其可以在一起工作. 二.适配器模式场景 要想只有USB接口的电脑想使用PS/2接口的键 ...