【题目】

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 解题报告的更多相关文章

  1. LeetCode: Sort Colors 解题报告

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

  2. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  3. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  4. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  5. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  6. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  7. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  8. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  9. 【LeetCode】148. Sort List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. tcpreplay工具使用

    参考:http://www.cnblogs.com/jiayy/p/3447047.html   速率控制算法的大体思路就是,通过适当的sleep,增加包发送的时间,从而减小算出来的速率,以达到用户设 ...

  2. spring的@Async异步使用

    pring的@Async功能,用的时候一定要注意: 1.异步方法和调用类不要在同一个类中. 2.xml里需要加入这一行 <task:annotation-driven/> 下面的可以直接粘 ...

  3. 关于XUtils框架细解

    感谢关注xUitls的网友最近一段时间给予的热心反馈,xUtils近期做了很多细节优化之后,功能和api已经稳定. 1.9.6主要更新内容:Bitmap加载动画有时重复出现的问题修复,加载过程优化; ...

  4. java 从网络Url中下载文件

    转自:http://blog.csdn.net/xb12369/article/details/40543649 /** * 从网络Url中下载文件 * @param urlStr * @param ...

  5. TCP三次握手连接和TCP四次挥手及大量TIME_WAIT解决方法:

    1.TCP建立连接,三次握手 建立的TCP连接可靠的连接,必须经过三次握手建立连接才能正式通信彼此传输数数据. 客户端请求服务端建立连接 第一次握手:客户给服务发送一个请求报文SYN, 客户端的状态置 ...

  6. 【java】Could not find or load main class

    https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean A comm ...

  7. 帮助自定义选择框样式的Javascript - DropKick.js

    来源:GBin1.com 在线演示  在线下载 当你想要设计一个页面样式时,没有什么比表单更让人头疼了.而当你设计一个表单的样式时,最让你头疼的就应该非下拉框<select>莫属了. 我们 ...

  8. ftp上传下载至网站

    完整的命令行模式解析! 1. 首先open 域名(Ip)形式即可 实例: open 60.205.45.115 2.后面输入用户名(主机名): bxw2713600302 3.输入密码:密码默认显示不 ...

  9. webDriver API——第6部分Locate elements By

    These are the attributes which can be used to locate elements. See the Locating Elements chapter for ...

  10. GitHub页面布局乱了,怎么解决??

    GitHub页面布局乱了,怎么解决?? GitHub乱了,怎么解决?? 一年一度的布局又乱了!!! F12一下下面有东西加载不了,,,, Refused to evaluate a string as ...