题目:

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三个元素的个数,然后根据各个元素的个数将原来的数组进行覆盖。这种方法需要对数组进行两轮遍历,第一轮遍历统计个数,第二轮遍历覆盖原数组。

改进想法:一次遍历即可将原数组排好序。一般来说排序算法都不太会是O(n)时间复杂度的,这道题之所以能做到这一点是因为只有3个元素。方法是:从前往后遍历数组,若遍历到0则将当前遍历到的0与从前往后最先的一个非0的数进行交换,若遍历到2则将当前遍历到的2与从后往前最先的一个非2的数进行交换,若遍历到1则继续往后遍历。对于0和2的情况,需要将当前元素与前面的或者后面的元素进行交换,所以需要用2个变量分别记住一前一后两个下标的位置,并不断进行更新。

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
var left=0,right=nums.length-1,zero=0,t; while(left<=right){
if(nums[left]==0){//nums[zero]=0
t=nums[left];
nums[left]=nums[zero];
nums[zero]=t;
zero++;
left++;
}else if(nums[left]==2){//nums[right]=2
t=nums[left];
nums[left]=nums[right];
nums[right]=t;
right--;
}else{
left++;
}
}
};

【数组】Sort Colors的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

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

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

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

  3. Sort Colors I & II

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

  4. 【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 ...

  5. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  6. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  7. 【LeetCode】Sort Colors 数组排序

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

  8. Sort colors系列

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

  9. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  10. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

随机推荐

  1. (匹配)Courses -- hdu --1083

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. hdu 2063 匈牙利算法

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 男女配对最大数 匈牙利算法模板 #include <cstdio> #include < ...

  3. underscore utility

    1._.noConflict:命名冲突处理方法 _.noConflict = function() { root._ = previousUnderscore; //返回this不错 return t ...

  4. 【Win10】页面的导航效果

    CommonNavigationTransitionInfo ContinuumNavigationTransitionInfo DrillInNavigationTransitionInfo Ent ...

  5. CentOS搭建NFS服务

    系统结构 ----------------------------------------------------------------------------------------------- ...

  6. Linux Mint 楷体问题

    很多人都遇见过刚装完的 Linux Mint 界面字体是黑体,然后莫名其妙就变成楷体的问题. 先不说技术层面的原因,只说怎么解决. 造成这种情况,多数是安装输入法或其他某种软件的时候,同时安装了 AR ...

  7. python 几种方法实现随机生成8位同时包含数字、大写字符、小写字符密码的小程序

    python 实现随机生成包8位包含大写字母.小写字母和数字的密码的程序.要求:1用户输入多少次就生成多少条密码,2要求密码必须同时包含大写字母.小写字母和数字,长度8位,不能重复代码如下: impo ...

  8. C# 读取Excel,一波华丽的操作

    C# 读取Excel,其实有很多方法.但是今天要来一波华丽的操作. 先看效果: 以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的. Exc ...

  9. c#字典怎么获取第一个键值 List<对象>获取重复项,转成Dictionary<key,List<对象>>

    c#字典怎么获取第一个键值 Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictio ...

  10. ORM到底是什么有何优缺点

    转载地址:http://www.cnblogs.com/wgbs25673578/p/5140482.html ORM的概念, ORM到底是什么 一.ORM简介         对象关系映射(Obje ...