您的位置:首页 > 其它

[刷题]Sort Colors II

2015-09-15 15:08 197 查看
[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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: