Skip to content

Commit 4649ab3

Browse files
committed
feat: add solutions to lc problem: No.0040
No.0040.Combination Sum II
1 parent 8c6f708 commit 4649ab3

File tree

6 files changed

+341
-63
lines changed

6 files changed

+341
-63
lines changed

solution/0000-0099/0040.Combination Sum II/README.md

+129-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@
3838
  [5]
3939
]</pre>
4040

41-
4241
## 解法
4342

43+
DFS 回溯法。需要先对 candidates 数组进行排序。
44+
45+
去重技巧:
46+
47+
```python
48+
if i > u and candidates[i] == candidates[i - 1]:
49+
continue
50+
```
51+
4452
<!-- 这里可写通用的实现逻辑 -->
4553

4654
<!-- tabs:start -->
@@ -50,15 +58,134 @@
5058
<!-- 这里可写当前语言的特殊实现逻辑 -->
5159

5260
```python
53-
61+
class Solution:
62+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
63+
def dfs(u, s, t):
64+
if s > target:
65+
return
66+
if s == target:
67+
ans.append(t[:])
68+
return
69+
for i in range(u, len(candidates)):
70+
if i > u and candidates[i] == candidates[i - 1]:
71+
continue
72+
t.append(candidates[i])
73+
dfs(i + 1, s + candidates[i], t)
74+
t.pop()
75+
76+
ans = []
77+
candidates.sort()
78+
dfs(0, 0, [])
79+
return ans
5480
```
5581

5682
### **Java**
5783

5884
<!-- 这里可写当前语言的特殊实现逻辑 -->
5985

6086
```java
87+
class Solution {
88+
private List<List<Integer>> ans;
89+
private int[] candidates;
90+
private int target;
91+
92+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
93+
ans = new ArrayList<>();
94+
Arrays.sort(candidates);
95+
this.target = target;
96+
this.candidates = candidates;
97+
dfs(0, 0, new ArrayList<>());
98+
return ans;
99+
}
100+
101+
private void dfs(int u, int s, List<Integer> t) {
102+
if (s > target) {
103+
return;
104+
}
105+
if (s == target) {
106+
ans.add(new ArrayList<>(t));
107+
return;
108+
}
109+
for (int i = u; i < candidates.length; ++i) {
110+
if (i > u && candidates[i] == candidates[i - 1]) {
111+
continue;
112+
}
113+
t.add(candidates[i]);
114+
dfs(i + 1, s + candidates[i], t);
115+
t.remove(t.size() - 1);
116+
}
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
vector<int> candidates;
127+
vector<vector<int>> ans;
128+
vector<int> t;
129+
int target;
130+
131+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
132+
sort(candidates.begin(), candidates.end());
133+
this->candidates = candidates;
134+
this->target = target;
135+
vector<int> t;
136+
dfs(0, 0, t);
137+
return ans;
138+
}
139+
140+
void dfs(int u, int s, vector<int>& t) {
141+
if (s > target) return;
142+
if (s == target)
143+
{
144+
ans.push_back(t);
145+
return;
146+
}
147+
for (int i = u; i < candidates.size(); ++i)
148+
{
149+
if (i > u && candidates[i] == candidates[i - 1]) continue;
150+
t.push_back(candidates[i]);
151+
dfs(i + 1, s + candidates[i], t);
152+
t.pop_back();
153+
}
154+
}
155+
};
156+
```
61157
158+
### **Go**
159+
160+
```go
161+
func combinationSum2(candidates []int, target int) [][]int {
162+
var ans [][]int
163+
var t []int
164+
sort.Ints(candidates)
165+
var dfs func(u, s int, t []int)
166+
dfs = func(u, s int, t []int) {
167+
if s > target {
168+
return
169+
}
170+
if s == target {
171+
cp := make([]int, len(t))
172+
copy(cp, t)
173+
ans = append(ans, cp)
174+
return
175+
}
176+
for i := u; i < len(candidates); i++ {
177+
if i > u && candidates[i] == candidates[i-1] {
178+
continue
179+
}
180+
t = append(t, candidates[i])
181+
dfs(i+1, s+candidates[i], t)
182+
t = t[:len(t)-1]
183+
}
184+
}
185+
186+
dfs(0, 0, t)
187+
return ans
188+
}
62189
```
63190

64191
### **...**

solution/0000-0099/0040.Combination Sum II/README_EN.md

+122-2
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,141 @@
4444
<li><code>1 &lt;= target &lt;= 30</code></li>
4545
</ul>
4646

47-
4847
## Solutions
4948

49+
DFS.
50+
5051
<!-- tabs:start -->
5152

5253
### **Python3**
5354

5455
```python
55-
56+
class Solution:
57+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
58+
def dfs(u, s, t):
59+
if s > target:
60+
return
61+
if s == target:
62+
ans.append(t[:])
63+
return
64+
for i in range(u, len(candidates)):
65+
if i > u and candidates[i] == candidates[i - 1]:
66+
continue
67+
t.append(candidates[i])
68+
dfs(i + 1, s + candidates[i], t)
69+
t.pop()
70+
71+
ans = []
72+
candidates.sort()
73+
dfs(0, 0, [])
74+
return ans
5675
```
5776

5877
### **Java**
5978

6079
```java
80+
class Solution {
81+
private List<List<Integer>> ans;
82+
private int[] candidates;
83+
private int target;
84+
85+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
86+
ans = new ArrayList<>();
87+
Arrays.sort(candidates);
88+
this.target = target;
89+
this.candidates = candidates;
90+
dfs(0, 0, new ArrayList<>());
91+
return ans;
92+
}
93+
94+
private void dfs(int u, int s, List<Integer> t) {
95+
if (s > target) {
96+
return;
97+
}
98+
if (s == target) {
99+
ans.add(new ArrayList<>(t));
100+
return;
101+
}
102+
for (int i = u; i < candidates.length; ++i) {
103+
if (i > u && candidates[i] == candidates[i - 1]) {
104+
continue;
105+
}
106+
t.add(candidates[i]);
107+
dfs(i + 1, s + candidates[i], t);
108+
t.remove(t.size() - 1);
109+
}
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
vector<int> candidates;
120+
vector<vector<int>> ans;
121+
vector<int> t;
122+
int target;
123+
124+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
125+
sort(candidates.begin(), candidates.end());
126+
this->candidates = candidates;
127+
this->target = target;
128+
vector<int> t;
129+
dfs(0, 0, t);
130+
return ans;
131+
}
132+
133+
void dfs(int u, int s, vector<int>& t) {
134+
if (s > target) return;
135+
if (s == target)
136+
{
137+
ans.push_back(t);
138+
return;
139+
}
140+
for (int i = u; i < candidates.size(); ++i)
141+
{
142+
if (i > u && candidates[i] == candidates[i - 1]) continue;
143+
t.push_back(candidates[i]);
144+
dfs(i + 1, s + candidates[i], t);
145+
t.pop_back();
146+
}
147+
}
148+
};
149+
```
61150
151+
### **Go**
152+
153+
```go
154+
func combinationSum2(candidates []int, target int) [][]int {
155+
var ans [][]int
156+
var t []int
157+
sort.Ints(candidates)
158+
var dfs func(u, s int, t []int)
159+
dfs = func(u, s int, t []int) {
160+
if s > target {
161+
return
162+
}
163+
if s == target {
164+
cp := make([]int, len(t))
165+
copy(cp, t)
166+
ans = append(ans, cp)
167+
return
168+
}
169+
for i := u; i < len(candidates); i++ {
170+
if i > u && candidates[i] == candidates[i-1] {
171+
continue
172+
}
173+
t = append(t, candidates[i])
174+
dfs(i+1, s+candidates[i], t)
175+
t = t[:len(t)-1]
176+
}
177+
}
178+
179+
dfs(0, 0, t)
180+
return ans
181+
}
62182
```
63183

64184
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
class Solution {
22
public:
3+
vector<int> candidates;
4+
vector<vector<int>> ans;
5+
vector<int> t;
6+
int target;
7+
38
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
4-
vector<vector<int>> ans;
5-
vector<int> tmp;
6-
sort(candidates.begin(),candidates.end());
7-
int len = candidates.size();
8-
9-
dfs(ans,tmp,candidates,target,len,0);
10-
9+
sort(candidates.begin(), candidates.end());
10+
this->candidates = candidates;
11+
this->target = target;
12+
vector<int> t;
13+
dfs(0, 0, t);
1114
return ans;
1215
}
13-
14-
void dfs(vector<vector<int>> &ans,vector<int> &tmp,vector<int> &nums,int target,int len,int index) {
15-
16-
if(target == 0){
17-
auto iter = find(ans.begin(),ans.end(),tmp);
18-
if(iter == ans.end())ans.push_back(tmp);
16+
17+
void dfs(int u, int s, vector<int>& t) {
18+
if (s > target) return;
19+
if (s == target)
20+
{
21+
ans.push_back(t);
22+
return;
23+
}
24+
for (int i = u; i < candidates.size(); ++i)
25+
{
26+
if (i > u && candidates[i] == candidates[i - 1]) continue;
27+
t.push_back(candidates[i]);
28+
dfs(i + 1, s + candidates[i], t);
29+
t.pop_back();
1930
}
20-
21-
for(int i = index;i<len && target >= nums[i];i++){
22-
tmp.push_back(nums[i]);
23-
dfs(ans,tmp,nums,target - nums[i],len,i+1);//注意i+1
24-
tmp.pop_back();
25-
}
2631
}
2732
};

0 commit comments

Comments
 (0)