• 【数组】Sort Colors


    题目:

    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++;
            }
        }
    };
  • 相关阅读:
    mysql数据库(12)--进阶二之索引
    mysql数据库(11)--进阶一之join
    mysql数据库(10)--变量、存储过程和函数
    mysql数据库(9)--视图
    mysql数据库(10)--limit与offset的用法
    前端常用在线引用地址
    SQL中ON和WHERE的区别
    jstl和e1
    解决中文乱码问题
    JSP
  • 原文地址:https://www.cnblogs.com/shytong/p/5107881.html
Copyright © 2020-2023  润新知