码迷,mamicode.com
首页 > 编程语言 > 详细

【数组】Sort Colors

时间:2016-01-07 00:56:40      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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

标签:

原文地址:http://www.cnblogs.com/shytong/p/5107881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!