题目:
设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。
示例:
输入: arr = [1,3,5,7,2,4,6,8], k = 4
输出: [1,2,3,4]
代码实现:
class Solution {标签:yyds,arr,示例,int,金典,个数,最小,vec,LeetCode From: https://blog.51cto.com/u_13321676/6140792
public int[] smallestK(int[] arr, int k) {
int[] vec = new int[k];
Arrays.sort(arr);
for (int i = 0; i < k; ++i) {
vec[i] = arr[i];
}
return vec;
}
}