题目:

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. cxf-rs 、spring 和 swagger 环境配置切换【github 有项目】

    环境切换的目的是 准生产和生产环境切换时,只修改一个文件就可以达到效果 在spring bean 文件中 配置: <bean class="cn.zno.common.context. ...

  2. 简单的nginx模拟网站的负载均衡

    环境:nginx1.10.3 虚拟机环境:3台centos7虚拟机 将148机器作为转发服务器配置如下 监听80端口,在http里面配置如下 将edc.com分别转发到149和150的服务器上 本地主 ...

  3. 记spring mvc传入List<Object>的一次尝试

    首先,看一段异常: org.springframework.http.converter.HttpMessageNotReadableException: Could not read documen ...

  4. Java火焰图在Netflix的实践

    转自 http://www.infoq.com/cn/news/2015/08/java-flamegraph 亲爱的读者:我们最近添加了一些个人消息定制功能,您只需选择感兴趣的技术主题,即可获取重要 ...

  5. Codeforces735D Taxes 2016-12-13 12:14 56人阅读 评论(0) 收藏

    D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  6. python关键的语法

    python关键的语法 1.标准类型分类

  7. What if you are involved in an automobile accident in the US

    What if you are involved in an automobile accident in the US With increasing Chinese tourists and vi ...

  8. 团队项目第六周——Alpha阶段项目复审(名字很难想队)

    Alpha阶段项目复审 小组 优点 缺点 排名 小谷围驻广东某工业719电竞大队 一个贴近大学生生活的二手交易平台.界面美观功能完善. 部分功能未完善,没有第三方登录 1 中午吃啥队 系统完善,界面简 ...

  9. python中list的sort方法

    转:https://www.cnblogs.com/zle1992/p/6271105.html 使用python对列表(list)进行排序,说简单也简单,说复杂也复杂,我一开始学的时候也搞不懂在说什 ...

  10. 403 - 禁止访问: 访问被拒绝(IIS)

    错误如下: 解决方法: 1,点击当前网站主页,选择默认文档 2,添加网站的起始页,我这里是main.htm