【LeetCode】Sort Colors 解题报告
【题目】
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?
题意就是对一个包括0,1,2三种数字的数组又一次排序,使得排好序的数组前一段都是0,中间一段都是1,最后一段都是2。
【扫描两遍的计数排序】
public class Solution {
public void sortColors(int[] A) {
int i, r, w, b;
r = w = b = 0;
for (i = 0; i < A.length; i++) {
if (A[i] == 0) r++;
else if (A[i] == 1) w++;
else b++;
}
for (i = 0; i < A.length; i++) {
if (i < r) A[i] = 0;
else if (i < r + w) A[i] = 1;
else A[i] = 2;
}
}
}
【扫描一遍。双向遍历】
从数组两端向中间遍历,前面放0。后面放2,。
把前面出现的2放到后面,后面出现的0放到前面。这样中间剩下的就是1。
用i, j两个指针遍历数组,r, b两个变量记录当前出现0和2的个数。也即放0和2的位置指针。
public class Solution {
public void swap(int[] A, int a, int b) {
int tmp = A[a];
A[a] = A[b];
A[b] = tmp;
}
public void sortColors(int[] A) {
int len = A.length;
int i, j, r, w, b;
i = 0;
j = len - 1;
r = b = 0;
while (i <= j) {
if (A[i] == 0) {
swap(A, i, r);
i++;
r++;
continue;
}
if (A[j] == 2) {
swap(A, j, len-1-b);
j--;
b++;
continue;
}
if (A[j] == 0) {
swap(A, i, j);
continue;
}
if (A[i] == 2) {
swap(A, i, j);
continue;
}
//假设上述不论什么情况都不满足,那么仅仅有以下一种可能
//if (A[i] == 1 && A[j] == 1) {
i++;
j--;
//}
}
}
}
【扫描一遍,单向遍历】
后来发现。从一个方向遍历更简单,由于双向遍历两个指针easy搞混,一个指针逻辑更清楚。
public class Solution {
public void swap(int[] A, int a, int b) {
int tmp = A[a];
A[a] = A[b];
A[b] = tmp;
}
public void sortColors(int[] A) {
int len = A.length;
int i, r = 0, b = 0;
for (i = 0; i < len-b; i++) {
if (A[i] == 0) {
swap(A, i, r);
r++;
} else if (A[i] == 2) {
swap(A, i, len-1-b);
b++;
i--; //后面交换过来的元素也要进行推断
}
}
}
}
【LeetCode】Sort Colors 解题报告的更多相关文章
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- LeetCode: Sort List 解题报告
Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 动画clip仅仅读的解决的方法,以及动画关键帧回调的办法
之前在Asset store上面下载了一个模型,有动画,可是想在Animation窗体编辑动画,插入关键帧的时候,出现了一点问题,发现动画切片是可读的. 在网上查了一下解决方式,后来在这里找到了答案: ...
- sql 改动表以及表字段
用SQL语句加入删除改动字段 1.添加字段 alter table docdsp add dspcode char(200) alter table tbl add meet ...
- Wishbone接口Altera JTAG UART
某些时候,我们在使用Altera FPGA的时候,尤其是涉及SoC系统的时候,通常需要一个串口与PC交互.使用Altera的USB-Blaster免去了外接一个串口.我们可以使用下面所述的IP核通过U ...
- 如何监控Oracle
介绍了DBA每天在监控Oracle数据库方面的职责,讲述了如何通过shell脚本来完成这些重复的监控工作.本文首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron来定时执行DB ...
- XmlRpc with C#/Java【转】
最近看了几个项目都是用xmlrpc协作完成的,就做了几个测试客户端和服务器端和大家一起分享.希望能对入门的同学有帮助 关于xmlrpc的介绍和规范参考http://www.xml-rpc.net/ 下 ...
- Java内存缓存
1.缓存为什么要存在 应用服务器资源是有限的,数据库每秒中接受请求的次数也是有限的.如果利用有限的资源来提供尽可能大的吞吐量呢,一个办法:减少计 算量,缩短请求流程(减少网络io或者硬盘io),这时候 ...
- [Android Pro] 注册 Google Play 开发者帐户
官网地址: https://support.google.com/googleplay/android-developer/answer/6112435?hl=zh-Hans 博客地址: http:/ ...
- Set 遍历的三种方法
1.迭代遍历:Set<String> set = new HashSet<String>();Iterator<String> it = set.iterator( ...
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat等使用详解
转载:http://blog.csdn.net/tzs_1041218129/article/details/61630981 javap 和 javac javap -help javac -hel ...