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?

 
我的解法是定义指针start = -1,指针end = n;一次遍历,如果遇到0,交换给start,遇到2,交换给end,遇到1别管。
class Solution {
public:
void sortColors(int A[], int n) {
if(n <= ) return;
int start = -, end = n;
int p = ;
while(p < n){
if(A[p] == ){
if(p > start) swap(A, ++start, p);
else ++p;
}else if(A[p] == ){
if(p < end) swap(A, p, --end);
else ++p;
}else ++p;
}
}
private:
void swap(int A[], int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
};

网上还有更加通用和直观的解法

 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]=;
}
} }

稍微简化一点:

public void sortColors(int[] A) {

    int i=-, j=-;

    for(int p = ; p < A.length; p++) {

       int v = A[p];
A[p] = ; if (v == ) { A[++j] = ;
A[++i] = ;
}
else if (v == ) { A[++j] = ;
}
}
}

[LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法的更多相关文章

  1. 统计数组中各个元素出现的次数,元素取值范围为:1到N

     问题描述: * 给定一个整数数组a,长度为N,元素取值范围为[1,N]. * 统计各个元素出现的次数,要求时间复杂度为O(N),空间复杂度为O(1). * 可以改变原来数组结构.  思路: * 从第 ...

  2. jQuery 表单元素取值与赋值方法总结

    一.普通文本框的赋值与取值 1.1.1赋值 <h2>jQuery 表单元素取值与赋值方法总结</h2> <input type="text" clas ...

  3. jQuery对html元素取值与赋值

    以下总结了常用的jQuery选择器对html元素取值与赋值 Textbox:  var str = $('#txt').val(); $('#txt').val("Set Lbl Value ...

  4. python迭代器-迭代器取值-for循环-生成器-yield-生成器表达式-常用内置方法-面向过程编程-05

    迭代器 迭代器 迭代: # 更新换代(其实也是重复)的过程,每一次的迭代都必须基于上一次的结果(上一次与这一次之间必须是有关系的) 迭代器: # 迭代取值的工具 为什么用迭代器: # 迭代器提供了一种 ...

  5. 寻找链表倒数第k个元素,只遍历一遍(编程之美)

    class LNode { public LNode next; public int data; } /*找出倒数第k个元素,只遍历一遍*/ class Kk { private static LN ...

  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 @ Python

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

  8. 假设数组a有n个元素,元素取值范围是1~n,如何判定数组是否存在重复元素

    方法一:位图法,原理是首先申请一个长度为n且均为’0’组成的字符串,字符串的下标即为数组a[]中的元素,然后从头开始遍历数组a[N],取每个数组元素的值,将其对应的字符串中的对应位置置1,如果已经置过 ...

  9. LeetCode: Sort Colors 解题报告

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

随机推荐

  1. 使用eclipse创建maven项目出现的一个问题

    错误信息 This error occurs when you employ a plugin that Maven could not download. Possible causes for t ...

  2. java一些面试题

    java虚拟机 什么时候会触发full gc System.gc()方法的调用 老年代空间不足 永生区空间不足(JVM规范中运行时数据区域中的方法区,在HotSpot虚拟机中又被习惯称为永生代或者永生 ...

  3. 收割大厂offer需要具备的条件

    转载出处 本人也一直在关注互联网,觉得还是有些了解.互联网要求是越来越高了,竞争的人太多了,不过你不用担心,个人觉得,你到了中层的水平,拿二线offer应该没问题,人多也有人多的好处,我比别人多努力一 ...

  4. Java语法基础课后作业

    1.动手动脑 运行它EnumTest.java,分析运行结果 s和t分别引用的是SMALL和LARGE,枚举类型不是原始数据类型,s和u的赋值方式不同,但结果一样,列出它的所有值:SMALL,MEDI ...

  5. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  6. 重新看《JavaScript高级程序设计》

    几点心得: 1)数据是基础,一共有3种基础数据:null.undefined.和object:遵循从无到有从简单到复杂的演变过程 2)衍生数据:衍生数据是指操作符合语句,这些是基础数据产生导致的必然结 ...

  7. 这些JavaScript编程黑科技,装逼指南,高逼格代码,让你惊叹不已

    Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...

  8. activeMQ 讲解及实战

    #### 软件架构项目中需要用到activeMQ 下载地址:http://activemq.apache.org/download.html #### 安装教程需要安装jdk环境activeMQ免安装 ...

  9. 使用WebClient类对网页下载源码,对文件下载保存及异步下载并报告下载进度

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx4AAAI7CAIAAADtTtpYAAAgAElEQVR4nO3dX6xlV33Y8f3UJFUqHq

  10. 动态include是通过servlet进行页面信息交互的

    动态include是通过servlet进行页面信息交互的