Given an array with n objects colored redwhite 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 01, and 2 to represent the color red, white, and blue respectively.

You should do it in-place (sort numbers in the original array).

Analysis:

Use two pointers p, q, p = 0 and q = A.length - 1. If 0 is found, swap 0 with the number pointed by p, then p++. If 2 is found, swap 2 with the number pointed by q, then q--.

 public class Solution {
public void sortColors(int[] A) {
if (A == null || A.length == ) return;
int leftPointer = , rightPointer = A.length - , current = ; while (current <= rightPointer) {
if (A[current] == ) {
swap(A, current, leftPointer);
leftPointer++;
current++; // we can garantee the left values are valid
} else if (A[current] == ) {
swap(A, current, rightPointer);
rightPointer--;
} else {
current++;
}
}
}
public void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}

Sort Colors

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.

Example

Given colors=[3, 2, 2, 1, 4]k=4, your code should sort colors in-place to[1, 2, 2, 3, 4].

分析: http://www.cnblogs.com/yuzhangcmu/p/4177326.html

inplace,并且O(N)时间复杂度的算法。

我们可以使用类似桶排序的思想,对所有的数进行计数。

1. 从左扫描到右边,遇到一个数字,先找到对应的bucket.比如

3 2 2 1 4

第一个3对应的bucket是index = 2 (bucket从0开始计算)

2. Bucket 如果有数字,则把这个数字移动到i的position(就是存放起来),然后把bucket记为-1(表示该位置是一个计数器,计1)。

3. Bucket 存的是负数,表示这个bucket已经是计数器,直接减1. 并把color[i] 设置为0 (表示此处已经计算过)

4. Bucket 存的是0,与3一样处理,将bucket设置为-1, 并把color[i] 设置为0 (表示此处已经计算过)

5. 回到position i,再判断此处是否为0(只要不是为0,就一直重复2-4的步骤)。

6.完成1-5的步骤后,从尾部到头部将数组置结果。(从尾至头是为了避免开头的计数器被覆盖)

例子(按以上步骤运算):

3 2 2 1 4

2 2 -1 1 4

2 -1 -1 1 4

0 -2 -1 1 4

-1 -2 -1 0 4

-1 -2 -1 -1 0

 class Solution {
/*
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == 0 || k <= 1) return; int len = colors.length;
for (int i = 0; i < len; i++) {
// Means need to deal with A[i]
while (colors[i] > 0) {
int num = colors[i];
if (colors[num - 1] > 0) {
// 1. There is a number in the bucket,
// Store the number in the bucket in position i;
colors[i] = colors[num - 1];
colors[num - 1] = -1;
} else {
// 2. Bucket is using or the bucket is empty.
colors[num - 1]--;
// delete the A[i];
colors[i] = 0;
}
}
}
int pointer = colors.length - 1;
for (int i = colors.length - 1; i >= 0; i--) {
int count = colors[i];
if (count == 0) continue; while (count < 0) {
colors[pointer--] = i + 1;
count++;
}
}
}
*/
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == || k <= ) return;
int[] count = new int[k];
int len = colors.length;
for (int i = ; i < len; i++) {
count[colors[i] - ]++;
}
int index = ;
for (int i = ; i < count.length; i++) {
int p = ;
while (p < count[i]) {
colors[index] = i + ;
index++;
p++;
}
}
}
}

Sort Colors I & II的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  2. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  3. LeetCode: Sort Colors 解题报告

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

  4. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  5. 【LeetCode】Sort Colors

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

  6. 52. Sort Colors && Combinations

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

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

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

  8. 【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 ...

  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. ognl用法 取变量时候 需要在变量前面加上# 取字符串需要用单引号包裹字符串

  2. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...

  3. P3216 [HNOI2011]数学作业

    题目描述 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M ,要求计算Concatenate (1 .. N) Mod M 的值,其中 Concatenat ...

  4. mysql的check约束问题

    mysql手册中写道:存储引擎会解析check子句,但是会把它忽略掉 The CHECK clause is parsed but ignored by all storage engines. 现在 ...

  5. MT【121】耐克数列的估计

    已知$\{a_n\}$满足$a_1=1,a_2=2,\dfrac{a_{n+2}}{a_n}=\dfrac{a_{n+1}^2+1}{a_n^2+1}$, 求$[a_{2017}]$_____ 解:容 ...

  6. 洛谷 P4009 汽车加油行驶问题 解题报告

    P4009 汽车加油行驶问题 题目描述 给定一个\(N×N\)的方形网格,设其左上角为起点◎,坐标(1,1) ,\(X\)轴向右为正,\(Y\)轴向下为正,每个方格边长为1 ,如图所示. 一辆汽车从起 ...

  7. google插件备份与安装

    说明 chrome浏览器中有很多有用的扩展程序, 但是可能因为某些原因我们不能直接去扩展程序商店进行下载获取, 如果我们自己电脑上已经通过某种方式添加了扩展程序, 想把它移植到其他没有FQ或者压根没有 ...

  8. 解题:USACO15JAN Grass Cownoisseur

    解题 首先缩点没啥可说的,然后考虑枚举这次逆行的边.具体来说在正常的图和反图上各跑一次最长路,然后注意减掉起点的贡献,用拓扑排序实现(我这里瞎写了个Bellman_Ford,其实在DAG上这好像和拓扑 ...

  9. python学习(21) smtp发送邮件

    原文链接: https://www.jianshu.com/p/369ec15bfe22 本文介绍python发送邮件模块smtplib以及相关MIME模块.smtplib用于生成邮件发送的代理,发送 ...

  10. unity还原three——顶点,三角面,uv

    public class Geometry { public Geometry(string name, Data data, Hashtable hash) { Debug.Log("解析 ...