|
61 | 61 |
|
62 | 62 | <p><meta charset="UTF-8" />注意:本题与主站 39 题相同: <a href="https://leetcode-cn.com/problems/combination-sum/">https://leetcode-cn.com/problems/combination-sum/</a></p>
|
63 | 63 |
|
64 |
| - |
65 | 64 | ## 解法
|
66 | 65 |
|
67 | 66 | <!-- 这里可写通用的实现逻辑 -->
|
68 | 67 |
|
| 68 | +DFS。 |
| 69 | + |
| 70 | +为了避免重复方案,需要定义一个搜索起点。 |
| 71 | + |
69 | 72 | <!-- tabs:start -->
|
70 | 73 |
|
71 | 74 | ### **Python3**
|
72 | 75 |
|
73 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 77 |
|
75 | 78 | ```python
|
76 |
| - |
| 79 | +class Solution: |
| 80 | + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: |
| 81 | + ans = [] |
| 82 | + n = len(candidates) |
| 83 | + |
| 84 | + def dfs(s, u, t): |
| 85 | + if s == target: |
| 86 | + ans.append(t.copy()) |
| 87 | + return |
| 88 | + if s > target: |
| 89 | + return |
| 90 | + for i in range(u, n): |
| 91 | + c = candidates[i] |
| 92 | + t.append(c) |
| 93 | + dfs(s + c, i, t) |
| 94 | + t.pop() |
| 95 | + |
| 96 | + dfs(0, 0, []) |
| 97 | + return ans |
77 | 98 | ```
|
78 | 99 |
|
79 | 100 | ### **Java**
|
80 | 101 |
|
81 | 102 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
82 | 103 |
|
83 | 104 | ```java
|
| 105 | +class Solution { |
| 106 | + private List<List<Integer>> ans; |
| 107 | + private int target; |
| 108 | + private int[] candidates; |
| 109 | + |
| 110 | + public List<List<Integer>> combinationSum(int[] candidates, int target) { |
| 111 | + ans = new ArrayList<>(); |
| 112 | + this.target = target; |
| 113 | + this.candidates = candidates; |
| 114 | + dfs(0, 0, new ArrayList<>()); |
| 115 | + return ans; |
| 116 | + } |
| 117 | + |
| 118 | + private void dfs(int s, int u, List<Integer> t) { |
| 119 | + if (s == target) { |
| 120 | + ans.add(new ArrayList<>(t)); |
| 121 | + return; |
| 122 | + } |
| 123 | + if (s > target) { |
| 124 | + return; |
| 125 | + } |
| 126 | + for (int i = u; i < candidates.length; ++i) { |
| 127 | + int c = candidates[i]; |
| 128 | + t.add(c); |
| 129 | + dfs(s + c, i, t); |
| 130 | + t.remove(t.size() - 1); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### **C++** |
| 137 | + |
| 138 | +```cpp |
| 139 | +class Solution { |
| 140 | +public: |
| 141 | + vector<vector<int>> ans; |
| 142 | + vector<int> candidates; |
| 143 | + int target; |
| 144 | + |
| 145 | + vector<vector<int>> combinationSum(vector<int>& candidates, int target) { |
| 146 | + this->candidates = candidates; |
| 147 | + this->target = target; |
| 148 | + vector<int> t; |
| 149 | + dfs(0, 0, t); |
| 150 | + return ans; |
| 151 | + } |
| 152 | + |
| 153 | + void dfs(int s, int u, vector<int>& t) { |
| 154 | + if (s == target) |
| 155 | + { |
| 156 | + ans.push_back(t); |
| 157 | + return; |
| 158 | + } |
| 159 | + if (s > target) return; |
| 160 | + for (int i = u; i < candidates.size(); ++i) |
| 161 | + { |
| 162 | + int c = candidates[i]; |
| 163 | + t.push_back(c); |
| 164 | + dfs(s + c, i, t); |
| 165 | + t.pop_back(); |
| 166 | + } |
| 167 | + } |
| 168 | +}; |
| 169 | +``` |
84 | 170 |
|
| 171 | +### **Go** |
| 172 | +
|
| 173 | +```go |
| 174 | +func combinationSum(candidates []int, target int) [][]int { |
| 175 | + var ans [][]int |
| 176 | +
|
| 177 | + var dfs func(s, u int, t []int) |
| 178 | + dfs = func(s, u int, t []int) { |
| 179 | + if s == target { |
| 180 | + ans = append(ans, append([]int(nil), t...)) |
| 181 | + return |
| 182 | + } |
| 183 | + if s > target { |
| 184 | + return |
| 185 | + } |
| 186 | + for i := u; i < len(candidates); i++ { |
| 187 | + c := candidates[i] |
| 188 | + t = append(t, c) |
| 189 | + dfs(s+c, i, t) |
| 190 | + t = t[:len(t)-1] |
| 191 | + } |
| 192 | + } |
| 193 | +
|
| 194 | + var t []int |
| 195 | + dfs(0, 0, t) |
| 196 | + return ans |
| 197 | +} |
85 | 198 | ```
|
86 | 199 |
|
87 | 200 | ### **...**
|
|
0 commit comments