题解 | 牛的品种排序III
牛的品种排序III
https://www.nowcoder.com/practice/f6ab3d7e20f54860886848f0a6374987
import java.util.*;
public class Solution {
public int[] sortCowsIII (int[] cows, int k) {
final int n = cows.length;
int[] cnt = new int[k];
for (int i = 0; i < n; ++i) {
++cnt[cows[i]];
}
int next = 0;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < cnt[i]; ++j) {
cows[next++] = i;
}
}
return cows;
}
}
