【题目】

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. HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)

    Lightning Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. 四种有能力取代Cookies的客户端Web存储方案

    目前在用户的网络浏览器中保存大量数据需要遵循几大现有标准,每一种标准都拥有自己的优势.短板.独特的W3C标准化状态以及浏览器支持级别.但无论如何,这些标准的实际表现都优于广泛存在的cookies机制. ...

  3. You must have a TTY to run sudo

    1.方法一,给命令行添加tty ssh -t user@foo.com 2.使用sudo visudo修改配置: Defaults:user !requiretty 表示用户user使用sudo命令时 ...

  4. RMAN 还原与恢复

    一. RMAN 还原与恢复基础 在RMAN 用于中,还原与恢复是两个不同的概念.还原(restore):指访问先前生成的备份,从中得到一个或多个对象,然后在磁盘上的某个位置还原这些对象.恢复(reco ...

  5. XSS之浪潮已经来临

    前些天和Roy厉在微博上聊到微信公众账号,我说我在辛苦运营“网站安全中心”这个账号呢,他说我这账号粉丝少是少了点,不过用户定位精确,我说我不希望精确,因为我在尽可能写科普,科普需要传播. Roy厉说过 ...

  6. stylus使用文档总结:内置方法+参数+条件+迭代+导入+继承

    一.内置方法 返回各种颜色的比重(如red(color)等) 颜色函数是CSS预处里器中内置的颜色函数功能,这些功能可以对颜色值进行处理,例如颜色的变亮.变暗.渐变颜色等处理十分的方便. lighte ...

  7. Hibernate异常:Unable to locate appropriate constructor on class

    异常信息:org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class o ...

  8. (转) java中try/catch性能和原理

    stackoverflow上有一个讨论,参与的人还挺多: https://stackoverflow.com/questions/141560/should-try-catch-Go-inside-o ...

  9. DefaultMessageStore-CommitLog-MapedFileQueue.allocateMapedFileService初始化链

    刚刚在研究rocketmq生成文件的源码.零时记录一下MapedFileQueue中属性AllocateMapedFileService allocateMapedFileService的初始化链. ...

  10. dubbo forbid 注意的几种方式

    1.检查所调用的项目模块是否起来了 2.如果起来后,检查该模块配置是否正确 3.服务端起来后与管理端的项目内容不一致(比如服务端增加了东西,管理端没有更新)