[刷题]Sort Colors II

来源:互联网 发布:java中数组特点 编辑:程序博客网 时间:2024/06/04 19:30

[LintCode]Sort Colors II

class Solution {    /**     * @param colors: A list of integer     * @param k: An integer     * @return: nothing     */    public void sortColors2(int[] colors, int k) {        // 2015-09-15        if (colors == null || colors.length == 0 || k < 2) {            return;        }                int[] list = new int[k];        for (int i = 0; i < colors.length; i++) {            list[colors[i] - 1]++;        }                int index = 0;        for (int i = 0; i < list.length; i++) {            for (int j = 0; j < list[i]; j++) {                colors[index++] = i + 1;            }        }        return;    }}


0 0
原创粉丝点击