【题目】

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. vscode快捷键-for mac

    默认显示当前所有页面: command p ?: 显示可操作方法 >: 打开命令面板, 同comand shift p : : 跳转到对应行数 @: 搜索并跳转到应变量或函数 @: : 同上,分 ...

  2. nodesj中 中间件express-session的理解

    1.为什么使用session? session运行在服务器端,当客户端第一次访问服务器时,可以将客户的登录信息保存. 当客户访问其他页面时,可以判断客户的登录状态,做出提示,相当于登录拦截. sess ...

  3. java多线程之Concurrent包

    1.在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题. 2.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的 ...

  4. android基础知识复习——RelativeLayout布局属性、背景、半透明设置(XML设置)

    转自:http://blog.csdn.net/fansongy/article/details/6817968 复习布局与XML,写了一个空的登录界面.XML的注释我写在当行的后面了.程序运行图: ...

  5. Julia:高性能 GPU 计算的编程语言

    Julia:高性能 GPU 计算的编程语言 0条评论 2017-10-31 18:02    it168网站 原创 作者: 编译|田晓旭 编辑: 田晓旭 [IT168 评论]Julia是一种用于数学计 ...

  6. linux缓存nscd

    1.安装  yum -y install nscd 2.配置文件: /etc/nscd.conf 3.缓存文件:缓存DB文件在/var/db/nscd下.可以通过nscd -g查看统计的信息 4.清除 ...

  7. Swift 版本号非常好的卡片切换效果基于ZLSwipeableView(相似于[陌陌点点][探探])

    这是我在简书的文章. http://www.jianshu.com/p/734962c9bbed

  8. Nginx部署前端代码实现前后端分离

    实现前后端分离,可以让前后端独立开发.独立部署.独立单测,双方通过JSON进行数据交互. 对于前端开发人员来说,不用每次调试都需要启动或配置Java/Tomcat运行环境:对于后端开发人员来说 ,也不 ...

  9. PySpider 框架爬虫错误 HTTP 599: SSL certificate problem: unable to get local issuer certificate解决方案

    首先pyspider all启动pyspider的所有服务,然后访问http://localhost:5000创建一个爬虫任务:taobaomm,点开任务链接编辑http://localhost:50 ...

  10. SQL中inner join,outer join和cross join的区别

    使用join连表,缺陷的情况下是inner join,开发中使用的left join和right join属于outer join,outer join还包括full join 现有两张表,Table ...