剑指offer 40.最小的k个数。双百0ms
作者:互联网
计数排序
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if(k == 0 || arr.length == 0) return new int[0];
int[] count = new int[10001];
for(int num : arr) count[num]++;
int[] result = new int[k];
int index = 0;
for(int i = 0; i < count.length; i++){
while(count[i]-- > 0 && index < k) result[index++] = i;
if(index == k) break;
}
return result;
}
}
标签:count,index,arr,offer,int,0ms,40,result,new 来源: https://blog.csdn.net/Split_token/article/details/122597056