【一次过】Lintcode 143. 排颜色 II

给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序。

样例

给出colors=[3, 2, 2, 1, 4]k=4, 你的代码应该在原地操作使得数组变成[1, 2, 2, 3, 4]

挑战

一个相当直接的解决方案是使用计数排序扫描2遍的算法。这样你会花费O(k)的额外空间。你否能在不使用额外空间的情况下完成?


解题思路1:

就如上所说使用计数排序,题目类似于Lintcode 148. 颜色分类

public class Solution {
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */
    public void sortColors2(int[] colors, int k) {
        // write your code here
        int[] nums = new int[k+1];
        
        for(int i : colors)
            nums[i]++;
        
        int index = 0;
        for(int i=1; i<=k; i++){
            while(nums[i] > 0){
                colors[index++] = i;
                nums[i]--;
            }
        }
    }
}

解题思路2:

手写双路快排

public class Solution {
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */
    public void sortColors2(int[] colors, int k) {
        // write your code here
        quickSort(colors, 0, colors.length-1);
    }
    
    private void quickSort(int[] colors, int l,int r){
        if(l >= r)
            return;
        
        int p = partition(colors, l, r);
        
        quickSort(colors, l, p-1);
        quickSort(colors, p+1, r);
    }
    
    private int partition(int[] colors, int l, int r){
        int randomIndex = l + (int)(Math.random()*(r-l+1));
        swap(colors, l, randomIndex);
        int v = colors[l];
        
        int i = l+1;
        int j = r;
        
        while(true){
            while(i <= r && colors[i]<v)
                i++;
            while(j >= l+1 && colors[j]>v)
                j--;
            
            if(i > j)
                break;
            
            swap(colors, i++, j--);
        }
        
        swap(colors, l, j);
        
        return j;
    }
    
    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值