|
41 | 41 |
|
42 | 42 | <p><meta charset="UTF-8" />注意:本题与主站 77 题相同: <a href="https://leetcode-cn.com/problems/combinations/">https://leetcode-cn.com/problems/combinations/</a></p>
|
43 | 43 |
|
44 |
| - |
45 | 44 | ## 解法
|
46 | 45 |
|
47 | 46 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 47 |
|
| 48 | +深度优先搜索 DFS。 |
| 49 | + |
49 | 50 | <!-- tabs:start -->
|
50 | 51 |
|
51 | 52 | ### **Python3**
|
52 | 53 |
|
53 | 54 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 55 |
|
55 | 56 | ```python
|
56 |
| - |
| 57 | +class Solution: |
| 58 | + def combine(self, n: int, k: int) -> List[List[int]]: |
| 59 | + res = [] |
| 60 | + |
| 61 | + def dfs(i, n, k, t): |
| 62 | + if len(t) == k: |
| 63 | + res.append(t.copy()) |
| 64 | + return |
| 65 | + for j in range(i, n + 1): |
| 66 | + t.append(j) |
| 67 | + dfs(j + 1, n, k, t) |
| 68 | + t.pop() |
| 69 | + |
| 70 | + dfs(1, n, k, []) |
| 71 | + return res |
57 | 72 | ```
|
58 | 73 |
|
59 | 74 | ### **Java**
|
60 | 75 |
|
61 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 77 |
|
63 | 78 | ```java
|
| 79 | +class Solution { |
| 80 | + public List<List<Integer>> combine(int n, int k) { |
| 81 | + List<List<Integer>> res = new ArrayList<>(); |
| 82 | + dfs(1, n, k, new ArrayList<>(), res); |
| 83 | + return res; |
| 84 | + } |
| 85 | + |
| 86 | + private void dfs(int i, int n, int k, List<Integer> t, List<List<Integer>> res) { |
| 87 | + if (t.size() == k) { |
| 88 | + res.add(new ArrayList<>(t)); |
| 89 | + return; |
| 90 | + } |
| 91 | + for (int j = i; j <= n; ++j) { |
| 92 | + t.add(j); |
| 93 | + dfs(j + 1, n, k, t, res); |
| 94 | + t.remove(t.size() - 1); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### **C++** |
| 101 | + |
| 102 | +```cpp |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + vector<vector<int>> combine(int n, int k) { |
| 106 | + vector<vector<int>> res; |
| 107 | + vector<int> t; |
| 108 | + dfs(1, n, k, t, res); |
| 109 | + return res; |
| 110 | + } |
| 111 | + |
| 112 | + void dfs(int i, int n, int k, vector<int> t, vector<vector<int>>& res) { |
| 113 | + if (t.size() == k) |
| 114 | + { |
| 115 | + res.push_back(t); |
| 116 | + return; |
| 117 | + } |
| 118 | + for (int j = i; j <= n; ++j) |
| 119 | + { |
| 120 | + t.push_back(j); |
| 121 | + dfs(j + 1, n, k, t, res); |
| 122 | + t.pop_back(); |
| 123 | + } |
| 124 | + } |
| 125 | +}; |
| 126 | +``` |
64 | 127 |
|
| 128 | +### **Go** |
| 129 | + |
| 130 | +```go |
| 131 | +func combine(n int, k int) [][]int { |
| 132 | + var res [][]int |
| 133 | + var t []int |
| 134 | + dfs(1, n, k, t, &res) |
| 135 | + return res |
| 136 | +} |
| 137 | + |
| 138 | +func dfs(i, n, k int, t []int, res *[][]int) { |
| 139 | + if len(t) == k { |
| 140 | + cp := make([]int, k) |
| 141 | + copy(cp, t) |
| 142 | + *res = append(*res, cp) |
| 143 | + return |
| 144 | + } |
| 145 | + for j := i; j <= n; j++ { |
| 146 | + t = append(t, j) |
| 147 | + dfs(j+1, n, k, t, res) |
| 148 | + t = t[:len(t)-1] |
| 149 | + } |
| 150 | +} |
65 | 151 | ```
|
66 | 152 |
|
67 | 153 | ### **...**
|
|
0 commit comments