题目:

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. AD采样的一个例子

    用122.88k时钟采样153.6k的信号

  2. DIV+CSS实战(四)

    一.说明 在上篇博文<DIV+CSS(三)>中,一个页面基本上展示出来了!下面实现以下页面上的一些功能,比方批量删除等功能.这里以批量删除为例,批量禁止,批量启用和批量删除差不多,只不过一 ...

  3. java使用WebUploader做大文件的分块和断点续传

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  4. 测试setsockopt设置超时是否生效代码

    // 测试setsockopt设置超时是否生效代码 #include <arpa/inet.h> #include <netinet/in.h> #include <st ...

  5. C++中的浮点数运算的误差测试分析

    C++中的浮点数运算的误差 项目中需要计算判定,采用的是float型,如: float a < yLing, 其中:a = 2.0, y则从1.0 + 0.2*n  当n = 4时,条件成立了? ...

  6. shell 文本处理——使用awk格式化时间戳

    date -d @时间戳 "+%Y-%m-%d %H:%M:%S" 也可以内置函数 awk '{print strftime("%Y-%m-%d %H:%M:%S&quo ...

  7. GitLab使用自定义端口

      Git支持两种地址访问方式,一种是:使用ssh协议,另一种是:使用http协议.   今天在部署Git服务器拉取和上传代码是出现了以下问题ssh: connect to host gitlab.d ...

  8. LeetCode149:Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  9. Netbeans8.1设置Consola字体并解决中文乱码问题

    netbeans是php非常好用的ide,并且还是免费的!但是好多字体不支持中文,会显示如下乱码: 解决方法如下: 通过修改jre的配置文件成功解决了这个问题. 1. 进入jdk安装目录下/jre/l ...

  10. Azure认知服务的实际应用-资讯采集推送

    Azure认知服务的实际应用-资讯采集推送 演示 实现的是通过使用各种azure服务,每天自动获取资讯.博客,定时推送到公众号的功能! 微信公众号搜索TechViews,或直接扫描二维码关注,每天推送 ...