Sort Colors I & II
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.
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.
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的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 【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 ...
- 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 ...
随机推荐
- 对\${ctx}的一点理解
一.\${ctx}与${pageContext.request.contextPath}的区别 相同点: \${ctx}和\${pageContext.request.contextPath}都是获取 ...
- Python编写登陆接口
实现功能: 输入用户名和密码 认证成功后显示欢迎信息 输错三次后锁定 # 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! #!/ ...
- AtCoder Grand Contest 003
AtCoder Grand Contest 003 A - Wanna go back home 翻译 告诉你一个人每天向哪个方向走,你可以自定义他每天走的距离,问它能否在最后一天结束之后回到起点. ...
- 响应式开发(六)-----Bootstrap CSS----------Bootstrap文本排版
Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans-serif 作为其默认的字体栈.使用 Bootstrap 的排版特性,您可以创建标题.段落.列 ...
- 【loj2586】【APIO2018】选圆圈
题目 有 \(n\) 个圆$c_1,c_2, \cdots , c_n $,执行如下的操作: 找到剩下的半径最大的圆删除并删除所有和它有交的其他并没有被删除的圆: 求每个圆是被那个圆删除的: $1 \ ...
- js replace如何实现replaceAll
js下string对象的replace方法的定义如下: stringObject.replace(regexp/substr,replacement) 其中: 参数 | ...
- 「Vue」程序式路由导航用法
1.button发起点击请求<mt-button type='primary' size='large' plain @click="topdcmt(id)">商品评论 ...
- JAVA 线程池基本总结
合理利用线程池能够带来三个好处. 第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 第三:提高线程 ...
- sql service ---- update和delete 误操作数据 ---- 恢复数据
原文出处:http://blog.csdn.net/dba_huangzj/article/details/8491327 问题: 经常看到有人误删数据,或者误操作,特别是update和delete的 ...
- CAAnimation保持动画结束时的效果
配置动画时,加上一下两句 animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards;