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. jmeter函数

    1.常用JMeter函数 1)__regexFunction 正则表达式函数可以使用正则表达式(用户提供的)来解析前面的服务器响应(或者是某个变量值).函数会返回一个有模板的字符串,其中携带有可变的值 ...

  2. http站点如何启用为https站点?对收录的影响

    首先看一下百度官方对https站点的态度:百度开放收录https站点公告 百度搜索再次推出:全面支持https页面直接收录:另外从相关性的角度,百度搜索引擎认为权值相同的站点,采用https协议的页面 ...

  3. hbase 跳转过滤器skipfilter

    用于跳过整个行键,需要和其他过滤器一起使用,本例SkipFilter和ValueFilter过滤器组合使用过滤不符合条件的行, 如果不配合SkipFiter,ValueFilter只过滤单元值包含的列 ...

  4. C++11并发编程个人小结

    thread_local变量在每个线程第一次执行到时初始化(类似static),并在每个线程各自累加,并在线程结束时释放. std::condition_variable:: wait(std::un ...

  5. BZOJ 3527 力 | FFT

    BZOJ 3527 力 | 分治 题意 给出数组q,$E_i = \sum_{i < j} \frac{q_i}{(i - j) ^ 2} - \sum_{i > j} \frac{q_i ...

  6. Backbone学习总结

    Backbone中文学习文档:http://www.css88.com/doc/backbone/ 来到公司已经有一段时间了,到现在深深的感觉到自己的能力弱的像只周黑鸭,又干涩又黝黑,充满了麻(手麻脑 ...

  7. Linux查看动态库.so导出函数列表

    https://blog.csdn.net/chrisnotfound/article/details/80662923

  8. bzoj 3224

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 16656  Solved: 7255[Submit][St ...

  9. JS--条件语句

    一.If条件判断 1.1 if条件 if(条件){ //js代码 } 1.2 if...else if(条件){ //js代码 }else { //js代码 } 1.3 if..else if..el ...

  10. python3.6爬虫总结-01

    1. HTTP 简介 HTTP常见状态码 200/OK: 请求成功 201/Created: 请求已被实现,且一个新资源已根据请求被建立,URI跟随Location头信息返回. 202/Accepte ...