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. Google Chrome 调试JS简单教程[更新]

    题外话,刚开始我写这篇内容只是将自己了解的一些知识放上来,不巧的是我分析了我的来访日志,很多朋友都有这个需求,为了大家没有白来,我决定充实下这篇文章.最近更新时间2014-02-14 chrome版本 ...

  2. 【转】简述configure、pkg-config、pkg_config_path三者的关系

    原文网址:http://www.mike.org.cn/articles/description-configure-pkg-config-pkg_config_path-of-the-relatio ...

  3. 使用AWK分析Oracle系统锁定、Hang状态

    在早期Oracle版本中,由于技术不成熟等原因,数据库锁定和僵死状态还是时有发生的.对待这些问题,老先生们的处理策略无外乎是“重启”和“考究”两种策略.所谓“重启”,通过强制的重启服务器或者数据库,将 ...

  4. Linux 利用busybox制作根文件系统

    busybox版本:1.17.3 官网下载路径:https://busybox.net/downloads/ 网盘下载路径:https://pan.baidu.com/s/1nvrEa73 密码:7y ...

  5. 机器学习(Machine Learning)&深度学习(Deep Learning)资料汇总 (上)

    转载:http://dataunion.org/8463.html?utm_source=tuicool&utm_medium=referral <Brief History of Ma ...

  6. laravel的中间件demo

    过滤器已经被废除...刚学才两天,蛋疼 创建一个中间件 ./artisan make:middleware TestMiddleware 大概代码 <?php namespace App\Htt ...

  7. emacs之开始就加载tag

    ~/emacsConfig/original-tags.el (setq tags-table-list ' ( "~/emacsConfig/etags/muduo" " ...

  8. css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Java中split的用法

    Java中的我们可以利用split把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项:java.lang.string.splitsplit 方法 ...

  10. 杂项:HTML5-1/3-发展历程

    ylbtech-杂项:HTML5-1/3-发展历程 万维网的核心语言.标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改(这是一项推荐标准.外语原文:W3C Recommendat ...