|
48 | 48 |
|
49 | 49 | ## 解法
|
50 | 50 |
|
| 51 | +DFS 回溯法。需要先对 candidates 数组进行排序。 |
| 52 | + |
| 53 | +去重技巧: |
| 54 | + |
| 55 | +```python |
| 56 | +if i > u and candidates[i] == candidates[i - 1]: |
| 57 | + continue |
| 58 | +``` |
| 59 | + |
51 | 60 | <!-- 这里可写通用的实现逻辑 -->
|
52 | 61 |
|
53 | 62 | <!-- tabs:start -->
|
|
57 | 66 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 67 |
|
59 | 68 | ```python
|
60 |
| - |
| 69 | +class Solution: |
| 70 | + def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]: |
| 71 | + def dfs(u, s, t): |
| 72 | + if s > target: |
| 73 | + return |
| 74 | + if s == target: |
| 75 | + ans.append(t[:]) |
| 76 | + return |
| 77 | + for i in range(u, len(candidates)): |
| 78 | + if i > u and candidates[i] == candidates[i - 1]: |
| 79 | + continue |
| 80 | + t.append(candidates[i]) |
| 81 | + dfs(i + 1, s + candidates[i], t) |
| 82 | + t.pop() |
| 83 | + |
| 84 | + ans = [] |
| 85 | + candidates.sort() |
| 86 | + dfs(0, 0, []) |
| 87 | + return ans |
61 | 88 | ```
|
62 | 89 |
|
63 | 90 | ### **Java**
|
64 | 91 |
|
65 | 92 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 93 |
|
67 | 94 | ```java
|
| 95 | +class Solution { |
| 96 | + private List<List<Integer>> ans; |
| 97 | + private int[] candidates; |
| 98 | + private int target; |
| 99 | + |
| 100 | + public List<List<Integer>> combinationSum2(int[] candidates, int target) { |
| 101 | + ans = new ArrayList<>(); |
| 102 | + Arrays.sort(candidates); |
| 103 | + this.target = target; |
| 104 | + this.candidates = candidates; |
| 105 | + dfs(0, 0, new ArrayList<>()); |
| 106 | + return ans; |
| 107 | + } |
| 108 | + |
| 109 | + private void dfs(int u, int s, List<Integer> t) { |
| 110 | + if (s > target) { |
| 111 | + return; |
| 112 | + } |
| 113 | + if (s == target) { |
| 114 | + ans.add(new ArrayList<>(t)); |
| 115 | + return; |
| 116 | + } |
| 117 | + for (int i = u; i < candidates.length; ++i) { |
| 118 | + if (i > u && candidates[i] == candidates[i - 1]) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + t.add(candidates[i]); |
| 122 | + dfs(i + 1, s + candidates[i], t); |
| 123 | + t.remove(t.size() - 1); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### **C++** |
| 130 | + |
| 131 | +```cpp |
| 132 | +class Solution { |
| 133 | +public: |
| 134 | + vector<int> candidates; |
| 135 | + vector<vector<int>> ans; |
| 136 | + vector<int> t; |
| 137 | + int target; |
| 138 | + |
| 139 | + vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { |
| 140 | + sort(candidates.begin(), candidates.end()); |
| 141 | + this->candidates = candidates; |
| 142 | + this->target = target; |
| 143 | + vector<int> t; |
| 144 | + dfs(0, 0, t); |
| 145 | + return ans; |
| 146 | + } |
| 147 | + |
| 148 | + void dfs(int u, int s, vector<int>& t) { |
| 149 | + if (s > target) return; |
| 150 | + if (s == target) |
| 151 | + { |
| 152 | + ans.push_back(t); |
| 153 | + return; |
| 154 | + } |
| 155 | + for (int i = u; i < candidates.size(); ++i) |
| 156 | + { |
| 157 | + if (i > u && candidates[i] == candidates[i - 1]) continue; |
| 158 | + t.push_back(candidates[i]); |
| 159 | + dfs(i + 1, s + candidates[i], t); |
| 160 | + t.pop_back(); |
| 161 | + } |
| 162 | + } |
| 163 | +}; |
| 164 | +``` |
68 | 165 |
|
| 166 | +### **Go** |
| 167 | +
|
| 168 | +```go |
| 169 | +func combinationSum2(candidates []int, target int) [][]int { |
| 170 | + var ans [][]int |
| 171 | + var t []int |
| 172 | + sort.Ints(candidates) |
| 173 | + var dfs func(u, s int, t []int) |
| 174 | + dfs = func(u, s int, t []int) { |
| 175 | + if s > target { |
| 176 | + return |
| 177 | + } |
| 178 | + if s == target { |
| 179 | + cp := make([]int, len(t)) |
| 180 | + copy(cp, t) |
| 181 | + ans = append(ans, cp) |
| 182 | + return |
| 183 | + } |
| 184 | + for i := u; i < len(candidates); i++ { |
| 185 | + if i > u && candidates[i] == candidates[i-1] { |
| 186 | + continue |
| 187 | + } |
| 188 | + t = append(t, candidates[i]) |
| 189 | + dfs(i+1, s+candidates[i], t) |
| 190 | + t = t[:len(t)-1] |
| 191 | + } |
| 192 | + } |
| 193 | +
|
| 194 | + dfs(0, 0, t) |
| 195 | + return ans |
| 196 | +} |
69 | 197 | ```
|
70 | 198 |
|
71 | 199 | ### **...**
|
|
0 commit comments