【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 ...
随机推荐
- Sqlserver数据库还原.bak文件失败的两个问题
一.SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样 解决方案:将数据库限制访问改为:SINGLE_USER 数据库-->属性-->选项-->状 ...
- 美国风投行业50年数据揭示的10条VC投资秘密法则
美国风投行业50年数据揭示的10条VC投资秘密法则 来源:金融女王(微信号:FintechQ) 作者:Hatim Tyabji & Vijay Sathe 本文编译自以下外媒文章: http ...
- 如何 RESTFul 你的服务(译)
原文:http://reynders.co/how-restful-is-your-service/发表于:2013年9月 如今无论是社交媒体平台还是企业解决方案,Web services都不出不在. ...
- (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
双向循环链表是基于双向链表的基础上实现的,和双向链表的操作差不多,唯一的区别就是它是个循环的链表,通过每个节点的两个指针把它们扣在一起组成一个环状.所以呢,每个节点都有前驱节点和后继节点(包括头节点和 ...
- unity reflection probe --- forward deferred transparent opaque
deferred 和forward reflection probe 的差别 deferred ref是逐像素的 forward是逐 obj 但我还遇到一个问题就是box projection ref ...
- linux grep的选项
grep -i 关闭大写和小写敏感性 grep -v 打印全部不包括. . 的行(屏蔽某些条目) grep -l 打印包括模式的文件名称 grep ...
- 【android开发】10款实用的Android UI工具,非常有用!
移动应用的UI设计就好似达摩克利斯之剑,一方面,一个视觉.交互.体验良好的UI可以加强应用在用户心目中的形象和识别性.而另一方面,一个体验糟糕的UI设计不仅无法让用户沉浸在应用中,还会造成用户对应用产 ...
- 算法笔记_116:算法集训之代码填空题集三(Java)
目录 1 数组转置 2 文件管理 3 显示为树形 4 杨辉三角系数 5 圆周率与级数 6 整数翻转 7 自行车行程 8 祖冲之割圆法 9 最大5个数 10 最大镜像子串 1 数组转置 编写程序将 ...
- 算法笔记_070:BellmanFord算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...
- Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)
Twelves Monkeys Time Limit: 5 Seconds Memory Limit: 32768 KB James Cole is a convicted criminal ...