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. grpc nodejs tools 安装问题

    grpc nodejs  应用安装提示错误: stack Error: EPERM: operation not permitted, utime '/usr/local/lib/node_modul ...

  2. c# String.Split数组省去空字符

    public static void test() { string txt = "hi[#b1.jpg][#b4.jpg]few[#b1-3.jpg]"; txt = " ...

  3. Tomcat最大连接数问题

    Tomcat的server.xml中Context元素的以下参数应该怎么配合适 <Connector port="8080" maxThreads="150&quo ...

  4. 一篇文章学LINQ(原创)

    本篇文章主要介绍linq的基本用法,采用sql和linq比较的方式由浅入深进行学习, 注意:此文章是根据真实表来进行案例说明,表结构如下:  表1:    Student(学生表)           ...

  5. MVC涉及RouteTable自定义路径

    Routing 到目前为止,我们已经解决了MVC的很多问题,但忽略了最基本的最重要的一个问题:当用户发送请求时,会发生什么? 最好的答案是“执行Action 方法”,但仍存在疑问:对于一个特定的URL ...

  6. bfs判断子图是否连通

    int judge() { int v[13] = { 0 }; queue<int> myq; myq.push(ans[0]); v[ans[0]] = 1; while (!myq. ...

  7. struts2学习(6)自定义拦截器-登录验证拦截器

    需求:对登录进行验证,用户名cy 密码123456才能登录进去:  登录进去后,将用户存在session中: 其他链接要来访问(除了登录链接),首先验证是否登录,对这个进行拦截: com.cy.mod ...

  8. 20165226 2017-2018-4 《Java程序设计》第9周学习总结

    20165226 2017-2018-4 <Java程序设计>第9周学习总结 教材学习内容总结 第十二章 URL类 构造方法1 try { URL url = new URL(" ...

  9. Java-Runoob:Java 日期时间

    ylbtech-Java-Runoob:Java 日期时间 1.返回顶部 1. Java 日期时间 java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实 ...

  10. 第十一章 Helm-kubernetes的包管理器(下)

    11.5.5 开发自己的chart k8s提供了大连官方的chart, 不过要部署微服务,还是需要开发自己的chart: 1  创建chart    Helm会帮助创建目录mychart,并生成各类c ...