From 6d7a1077b43e655e134a5c42710ad354ba81cf29 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 19 Jul 2021 13:13:15 +0800 Subject: [PATCH] =?UTF-8?q?77:=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/Combinations.java | 96 +++++++++++++++++++ .../java/leetcode/editor/cn/Combinations.md | 35 +++++++ 2 files changed, 131 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/Combinations.java create mode 100644 src/main/java/leetcode/editor/cn/Combinations.md diff --git a/src/main/java/leetcode/editor/cn/Combinations.java b/src/main/java/leetcode/editor/cn/Combinations.java new file mode 100644 index 0000000..381ada9 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/Combinations.java @@ -0,0 +1,96 @@ +//给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 +// +// 你可以按任何顺序返回答案。 +// +// +// +// 示例 1: +// +// +//输入: n = 4, k = 2 +//输出: +//[ +// [2,4], +// [3,4], +// [2,3], +// [1,2], +// [1,3], +// [1,4], +//] +// +// 示例 2: +// +// +//输入: n = 1, k = 1 +//输出: [[1]] +// +// +// +// 提示: +// +// +// 1 <= n <= 20 +// 1 <= k <= n +// +// Related Topics 数组 回溯 +// 👍 626 👎 0 + +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +//77:组合 +public class Combinations { + public static void main(String[] args) { + //测试代码 + Solution solution = new Combinations().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + List> list = new ArrayList<>(); +// public List> combine(int n, int k) { +// int[] nums = new int[n]; +// for (int i = 0; i < n; i++) { +// nums[i] = i + 1; +// } +// dfs(nums, new ArrayList<>(), k); +// return list; +// } +// +// private void dfs(int[] nums, List temp, int k) { +// if (k == temp.size()) { +// list.add(new ArrayList<>(temp)); +// return; +// } +// +// for (int num : nums) { +// if (temp.size() == 0 || num > temp.get(temp.size() - 1)) { +// temp.add(num); +// dfs(nums, temp, k); +// temp.remove(temp.size() - 1); +// } +// } +// } + public List> combine(int n, int k) { + dfs(n, k, 1, new ArrayList<>()); + return list; + } + + public void dfs(int n, int k, int cur, List list) { + if (k == 0) { + this.list.add(new ArrayList<>(list)); + return; + } + for (int i = cur; i <= n - k + 1; i++) { + list.add(i); + dfs(n, k - 1, i + 1, list); + list.remove(list.size() - 1); + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/Combinations.md b/src/main/java/leetcode/editor/cn/Combinations.md new file mode 100644 index 0000000..880f45b --- /dev/null +++ b/src/main/java/leetcode/editor/cn/Combinations.md @@ -0,0 +1,35 @@ +

给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

+ +

你可以按任何顺序返回答案。

+ +

 

+ +

示例 1:

+ +
+输入: n = 4, k = 2
+输出:
+[
+  [2,4],
+  [3,4],
+  [2,3],
+  [1,2],
+  [1,3],
+  [1,4],
+]
+ +

示例 2:

+ +
+输入: n = 1, k = 1
+输出: [[1]]
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 20
  • +
  • 1 <= k <= n
  • +
+
Related Topics
  • 数组
  • 回溯
  • \n
  • 👍 626
  • 👎 0
  • \ No newline at end of file