颜色分类 II

本文介绍了一种解决给定数组中颜色分类问题的算法,要求相同颜色的对象相邻且按颜色编号排序,不使用内置排序函数。通过示例展示了如何使用自定义的分桶和交换策略实现这一功能。

颜色分类 II

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

不能使用代码库中的排序函数来解决这个问题
k <= n
样例
样例1
输入: 
[3,2,2,1,4] 
4
输出: 
[1,2,2,3,4]

样例2
输入: 
[2,1,1,2,2] 
2
输出: 
[1,1,2,2,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
        if(colors.length == 0) {
            return ;
        }

        sort(colors, 0, colors.length - 1, 1, k);
    }

    private void sort(int[] nums, int start, int end, int cStart, int cEnd) {
        if(start > end || cStart >= cEnd) {
            return ;
        }

        int mid = (cStart + cEnd) / 2;
        int l = start, r = end;
        while(l < r) {
            while(l < r && nums[r] > mid) {
                r--;
            }
            while(l < r && nums[l] <= mid) {
                l++;
            }

            if(l < r) {
                int tmp = nums[l];
                nums[l] = nums[r];
                nums[r] = tmp;

                l++;
                r--;
            }
        }

        sort(nums, start, l, cStart, mid);
        sort(nums, r, end, mid + 1, cEnd);
    }
}
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
        if(colors.length == 0) {
            return ;
        }

        sort(colors, 0, colors.length - 1, 1, k);
    }

    private void sort(int[] nums, int start, int end, int cStart, int cEnd) {
        if(start >= end || cStart >= cEnd) {
            return ;
        }

        int mid = (cStart + cEnd) / 2;
        int l = start, r = end;
        while(l <= r) {
            while(l <= r && nums[r] > mid) {
                r--;
            }
            while(l <= r && nums[l] <= mid) {
                l++;
            }

            if(l <= r) {
                int tmp = nums[l];
                nums[l] = nums[r];
                nums[r] = tmp;

                l++;
                r--;
            }
        }

        sort(nums, start, r, cStart, mid);
        sort(nums, l, end, mid + 1, cEnd);
    }
}
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 bucket[] = new int[k + 1];
        for(int i = 0; i < colors.length; i++) {
            bucket[colors[i]]++;
        }

        int cur = 0;
        for(int i = 0; i < bucket.length; i++) {
            while(bucket[i] != 0) {
                colors[cur++] = i;
                bucket[i]--;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值