【题目】

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. JavaScript入门详解

    开始开发工作. 一.Alert Confirm Prompt <html> <head> <script type="text/javascript" ...

  2. 在代码中加载storyBoard中的ViewController

    首先, 要在storyBoard中画出想要的VC, 然后建一个VC类和他关联.如图 : 调用时找如下写: DetailViewController *detailVC = [[UIStoryboard ...

  3. jquery动态添加表单数据

    动态添加用户 实现代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...

  4. Qt 5.7 亮瞎眼的更新

    Qt 5.7的beta版已经出来了,这将是一个超级重大的更新,主要有几个商业版的模块在GPLv3 open source 版的用户也可以用了,其中包括了两个很炫酷的模块: Qt Charts Qt D ...

  5. Ubuntu 13 Zookeeper 集群配置

    一. 目标 在Linux集群搭建Zookeeper集群,并配置管理Web管理系统. 二. 先决条件 本文档中基于VirtualBox Ubuntu Server 13.04 虚机(启动SSH服务器)构 ...

  6. 3D游戏图形技术解析(7)——视差映射贴图(Parallax Mapping)【转】

    http://www.cnblogs.com/taotaobujue/articles/2781371.html 视差映射贴图(Parallax Mapping) ● 传统纹理贴图的弊端 纹理贴图大家 ...

  7. Hive计算的临时文件清理

    hive 的存储路径的 .hive-staging_hive_yyyy-MM-dd_HH-mm-ss_SSS_xxxx-x 文件可以清理掉吗 https://blog.csdn.net/sparkex ...

  8. zabbix乱码问题

    Zabbix页面遇到历史记录的乱码需要修改数据库: 解决办法: 1.将 zabbix 数据库中的表备份: 2.手动删除 zabbix 数据库: 3.重新创建 zabbix 库时手动指定字符集为 utf ...

  9. iOS extern 和 #define 使用

    Easiest way: // Prefs.h#define PREFS_MY_CONSTANT @"prefs_my_constant" Better way: // Prefs ...

  10. 近期面试Android的一些面试题

    近期一个多月面试过一下公司(均为实习): 腾讯:内推一面卒. 正式校招拿到offer 阿里:内推二面卒. 蘑菇街:面完三面技术面,等待HR面 网易:拿到offer. 能够看到,大部分问题不难,可是能回 ...