leet-code/src/main/java/leetcode/editor/cn/SmallestKLcci.java

38 lines
936 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//设计一个算法找出数组中最小的k个数。以任意顺序返回这k个数均可。
//
// 示例:
//
// 输入: arr = [1,3,5,7,2,4,6,8], k = 4
//输出: [1,2,3,4]
//
//
// 提示:
//
//
// 0 <= len(arr) <= 100000
// 0 <= k <= min(100000, len(arr))
//
// Related Topics 数组 分治 快速选择 排序 堆(优先队列) 👍 92 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//面试题 17.14:最小K个数
class SmallestKLcci {
public static void main(String[] args) {
//测试代码
Solution solution = new SmallestKLcci().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] smallestK(int[] arr, int k) {
Arrays.sort(arr);
return Arrays.copyOf(arr, k);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}