From a242551365bb0e96efa1c518e6867580e5c5c7c9 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Fri, 3 Sep 2021 09:02:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2017.14:=E6=9C=80?= =?UTF-8?q?=E5=B0=8FK=E4=B8=AA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/SmallestKLcci.java | 38 +++++++++++++++++++ .../editor/cn/doc/content/SmallestKLcci.md | 15 ++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/SmallestKLcci.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/SmallestKLcci.md diff --git a/src/main/java/leetcode/editor/cn/SmallestKLcci.java b/src/main/java/leetcode/editor/cn/SmallestKLcci.java new file mode 100644 index 0000000..2c3e636 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SmallestKLcci.java @@ -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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/SmallestKLcci.md b/src/main/java/leetcode/editor/cn/doc/content/SmallestKLcci.md new file mode 100644 index 0000000..a63126c --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/SmallestKLcci.md @@ -0,0 +1,15 @@ +

设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。

+ +

示例:

+ +
输入: arr = [1,3,5,7,2,4,6,8], k = 4
+输出: [1,2,3,4]
+
+ +

提示:

+ + +
Related Topics
  • 数组
  • 分治
  • 快速选择
  • 排序
  • 堆(优先队列)

  • 👍 92
  • 👎 0
  • \ No newline at end of file