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?

Hide Tags

Array Two Pointers Sort

 

 
  这题还是挺简单的,就是对只有3个类型(0 1 2)的排序,题目中给了一个两次遍历的排序方法,即统计出现的次数然后填回数组,要求是遍历一次完成,看了一下discuss ,实现逻辑类似,即写的如何。
 
思路:
  1. 创建一个zeroidx 表示0 的末尾下标,初始化为0,twoidx 表示2的下标,初始化为n,不同地方是前者操作时++,后者--。
  2. 遍历数组如果遇到1,则继续遍历。
  3. 如果遇到0,则替换 zeroidx 与i 上的值,然后zerosidx +1,如果此时该位置上的值位0,则数组从0 to i 上为0,所以zeroidx = i+1.
  4. 如果遇到2,则 twoidx -1, 然后替换 twoidx 与i 的值,因为替换后的i 位置上的值未判断,所以i-1 进行多一次该位置上的遍历。

3.中zeroidx 可以一直+1 不直接跳位,这样需要多次交换,discuss 实现多是这样。

下面是我写的代码:

 #include <iostream>
using namespace std; class Solution {
public:
void sortColors(int A[], int n) {
int zeroIdx = ;
int twoIdx = n;
for(int i =;i<n&&i<twoIdx;i++){
if(A[i]==){
continue;
}
if(A[i]==){
A[i] = A[zeroIdx];
A[zeroIdx] = ;
if(++zeroIdx<n&&A[zeroIdx]==) zeroIdx=i+;
}
else if(A[i]==){
twoIdx--;
A[i]=A[twoIdx];
A[twoIdx]=;
i--;
}
else
return ;
}
return ;
}
}; int main()
{
int a[] = {,,,};
Solution sol;
sol.sortColors(a,sizeof(a)/sizeof(int));
for(int i=;i<sizeof(a)/sizeof(int);i++)
cout<<a[i]<<" ";
cout<<endl;
return ;
}

discuss 中有一个实现非常不错,逻辑清晰,可以将变量数3 扩展为k 个,只要不怕写起来麻烦,其逻辑类似于插入排序,将遍历的项插入正确的位置:

  1. 为3个(k个) 变量创建index a b c= -1;
  2. 遍历数组,如果为0,顺序修改a[++c] a[++b] a[++a],这样如果abc 一样,最终只有修改了 a[++a] 这一项,然后同时又更新了3者的idx。
  3. 如果为1,则顺序修改 a[++c] a[++b],这样 0的index 未变。
  4. 如果为2,则顺序修改 a[++c]。
  public void sortColors(int[] A) {

     int i=-, j=-, k=-;

     for(int p = ; p < A.length; p++)
{
if(A[p] == )
{
A[++k]=;
A[++j]=;
A[++i]=;
}
else if (A[p] == )
{
A[++k]=;
A[++j]=; }
else if (A[p] == )
{
A[++k]=;
}
} }
 
 
 
 
 
 
 
 
 

[LeetCode] Sort Colors 只有3个类型的排序的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. [LeetCode] Sort Colors 颜色排序

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

  3. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  4. [Leetcode] Sort Colors (C++)

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

  5. 75.[LeetCode] Sort Colors

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

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

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

  7. LeetCode Sort Colors (技巧)

    题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...

  8. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  9. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

随机推荐

  1. 01_3_创建一个Action

    01_3_创建一个Action 1. 定义一个action 具体视图的返回可以由用户自己定义的Action来决定 具体的手段是根据返回的字符串找到相应的配置项,来决定视图的内容 具体Action的实现 ...

  2. 关于web安全--CSRF和XSS

    CSRF:跨站请求伪造. 攻击原理:一个用户登陆了可信的网站A,身份验证后A会下发一个cookie:此时用户又打开了另一个危险网站B,B引诱用户点击连接(该链接会访问A的接口),由于此时会携带cook ...

  3. 创建自定义 Estimator

    ref 本文档介绍了自定义 Estimator.具体而言,本文档介绍了如何创建自定义 Estimator 来模拟预创建的 Estimator DNNClassifier 在解决鸢尾花问题时的行为.要详 ...

  4. 解决iPhone滑动不流畅问题

    前段时间在做一个手机端的页面时遇到了iOS上滑动不流畅的问题,后来才发现安卓上没有问题,才意识到这是兼容性问题引起的,所以遇到问题后快速定位到问题根源非常重要.在网上一搜就找到了解决方案.以后遇到类似 ...

  5. vue 项目白屏解决方案

    在做的项目是使用 vue-cli 脚手架为基础的,只能使用微信浏览器打开的.在某次更新功能代码后,被反馈在一些手机上会出现白屏.经过一番探索,多管齐下解决了问题 白屏可能的原因: es6 代码没有被编 ...

  6. angular5 HttpInterceptor使用

    HttpInterceptor接口是ng的http请求拦截器,当需要拦截http请求,可以实现该接口. 1.创建HttpInterceptor 的实现类,并使用@Injectable()注解 @Inj ...

  7. python3 输入某年某月某日,判断这一天是这一年的第几天?

    题目 输入某年某月某日,判断这一天是这一年的第几天? 程序分析 特殊情况,闰年时需考虑二月多加一天. 代码: import calendar year = int(input("Year:& ...

  8. Python基础——列表(list)

    创建列表(list) 通过[]来创建list结构,里面放任何类型都可以,没有长度限制. list1=[] type(list1) list1=[1,2,3,4] list1 list1=['] lis ...

  9. set 方法总结整理

    #!/usr/bin/env python __author__ = "lrtao2010" #Python 3.7.0 集合常用方法 #集合是无序的,元素不能重复,元素只能是数字 ...

  10. 【HIHOCODER 1176】 欧拉路·一

    描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的角色来到了一个很大的湖边.湖上有N个小岛(编号1..N),以及连接小岛的 ...