面试题 17.14:最小K个数

This commit is contained in:
huangge1199 2021-09-03 09:02:17 +08:00
parent 75a1cf370d
commit a242551365
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,38 @@
//设计一个算法找出数组中最小的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)
}

View File

@ -0,0 +1,15 @@
<p>设计一个算法找出数组中最小的k个数。以任意顺序返回这k个数均可。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> arr = [1,3,5,7,2,4,6,8], k = 4
<strong>输出:</strong> [1,2,3,4]
</pre>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= len(arr) &lt;= 100000</code></li>
<li><code>0 &lt;= k &lt;= min(100000, len(arr))</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>快速选择</li><li>排序</li><li>堆(优先队列)</li></div></div><br><div><li>👍 92</li><li>👎 0</li></div>