题目描述

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的序列按顺序排好。
【扫描两遍的计数排序】

  1. public class Solution {
  2. public void sortColors(int[] A) {
  3. int i, r, w, b;
  4. r = w = b = 0;
  5. for (i = 0; i < A.length; i++) {
  6. if (A[i] == 0) r++;
  7. else if (A[i] == 1) w++;
  8. else b++;
  9. }
  10. for (i = 0; i < A.length; i++) {
  11. if (i < r) A[i] = 0;
  12. else if (i < r + w) A[i] = 1;
  13. else A[i] = 2;
  14. }
  15. }
  16. }

【扫描一遍,单向遍历】

注意,l记录0区,r记录2区的边界。因此循环遍历到i<=r即可。

  1. class Solution {
  2. public:
  3. void swap(int A[], int i, int j){
  4. int tmp=A[i];
  5. A[i]=A[j];
  6. A[j]=tmp;
  7. }
  8. void sortColors(int A[], int n) {
  9. int l=,r=n-;
  10. for(int i=;i<=r;i++){
  11. if(A[i]==){
  12. swap(A,i,l);
  13. l++;
  14. }
  15. else if(A[i]==){
  16. swap(A,i,r);
  17. r--;
  18. i--;
  19. }
  20. }
  21. }
  22. };

sort-colors——排序3种数字的更多相关文章

  1. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  2. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  3. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  5. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  6. Sort colors系列

    75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第 ...

  7. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  8. Sort Colors I & II

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

随机推荐

  1. 学习ExtJS4 常用控件

    ExtJS组件配置方式介绍 1.使用逗号分隔参数列表配置组件    首先来看一个简单的逗号分隔参数列表的例子.这个例子非常简单,它用来显示信息提示框. 2.使用Json对象配置组件  接下来看一个使用 ...

  2. 转载:Posix线程编程指南(2)

    概念及作用 在单线程程序中,我们经常要用到"全局变量"以实现多个函数间共享数据.在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程所共有.但有时应用程序设计中有必要提供 ...

  3. masscan banners 不显示

    https://github.com/robertdavidgraham/masscan/issues/221

  4. Appium+python自动化1-环境搭建(上)【转载】

    前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.小编擅长Python,所以接下来的教程是appium+py ...

  5. Android 利用Sharp样式设置文本框EditText圆角形状

    1.首先新建样式文件editsharp.xml: <?xml version="1.0" encoding="utf-8"?> <shape ...

  6. 写给小白看的 JavaScript 异步

    某天突然写了个方法要从后台调用数据,显示在前台页面,但是输出结果总是空 undefined,得不到数据.多方找资料才发现,原来是入了 JS 异步的 “坑”. 我们常常听到单线程.多线程.同步.异步这些 ...

  7. SpringMVC + Hibernate + MySQL 的简易网页搭建(Dao层 实现篇)

    首先在搭建一个网站后台前,需要明确出一个合理的网页搭建的结构框架,即从DB  ==>  Dao层 ==>Service层 ==>Control层 ==>View层(该层严格意义 ...

  8. 51nod 1873 初中的算术【Java BigDecimal/高精度小数】

    1873 初中的算术 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 Noder现在上初三了,正在开始复习中考.他每天要计算型如 (a× a× a× ...

  9. Longest Common Substring($LCS$)

    Longest Common Substring(\(LCS\)) 什么是子序列? 子序列就是某一个序列的不连续的一部分. 如图, \(abcde\)就是图中序列的一个子序列. 公共子序列 公共子序列 ...

  10. C语言基础之自增自减运算符及注意事项

    1.具体用法 1: int b; 2: int a = 10; 3: // b = 10 + 12; 4: //b = (a++) + (++a); 5: 6: // b = 11 + 11; 7: ...