Skip to content

Commit ea1b388

Browse files
committed
feat: add solutions to lc/lcof2 problem
lc No.0039 & lcof2 No.081. Combination Sum
1 parent 844572e commit ea1b388

File tree

12 files changed

+587
-35
lines changed

12 files changed

+587
-35
lines changed

lcof2/剑指 Offer II 081. 允许重复选择元素的组合/README.md

+115-2
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,140 @@
6161

6262
<p><meta charset="UTF-8" />注意:本题与主站 39&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/combination-sum/">https://leetcode-cn.com/problems/combination-sum/</a></p>
6363

64-
6564
## 解法
6665

6766
<!-- 这里可写通用的实现逻辑 -->
6867

68+
DFS。
69+
70+
为了避免重复方案,需要定义一个搜索起点。
71+
6972
<!-- tabs:start -->
7073

7174
### **Python3**
7275

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

7578
```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
7798
```
7899

79100
### **Java**
80101

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

83104
```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+
```
84170
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+
}
85198
```
86199

87200
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> ans;
4+
vector<int> candidates;
5+
int target;
6+
7+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
8+
this->candidates = candidates;
9+
this->target = target;
10+
vector<int> t;
11+
dfs(0, 0, t);
12+
return ans;
13+
}
14+
15+
void dfs(int s, int u, vector<int>& t) {
16+
if (s == target)
17+
{
18+
ans.push_back(t);
19+
return;
20+
}
21+
if (s > target) return;
22+
for (int i = u; i < candidates.size(); ++i)
23+
{
24+
int c = candidates[i];
25+
t.push_back(c);
26+
dfs(s + c, i, t);
27+
t.pop_back();
28+
}
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class Solution
6+
{
7+
public IList<IList<int>> CombinationSum(int[] candidates, int target)
8+
{
9+
Array.Sort(candidates);
10+
candidates = candidates.Distinct().ToArray();
11+
12+
var paths = new List<int>[target + 1];
13+
paths[0] = new List<int>();
14+
foreach (var c in candidates)
15+
{
16+
for (var j = c; j <= target; ++j)
17+
{
18+
if (paths[j - c] != null)
19+
{
20+
if (paths[j] == null)
21+
{
22+
paths[j] = new List<int>();
23+
}
24+
paths[j].Add(c);
25+
}
26+
}
27+
}
28+
29+
var results = new List<IList<int>>();
30+
if (paths[target] != null) GenerateResults(results, new Stack<int>(), paths, target, paths[target].Count - 1);
31+
return results;
32+
}
33+
34+
private void GenerateResults(IList<IList<int>> results, Stack<int> result, List<int>[] paths, int remaining,
35+
int maxIndex)
36+
{
37+
if (remaining == 0)
38+
{
39+
results.Add(new List<int>(result));
40+
return;
41+
}
42+
for (var i = maxIndex; i >= 0; --i)
43+
{
44+
var value = paths[remaining][i];
45+
result.Push(value);
46+
var nextMaxIndex = paths[remaining - value].BinarySearch(value);
47+
if (nextMaxIndex < 0)
48+
{
49+
nextMaxIndex = ~nextMaxIndex - 1;
50+
}
51+
GenerateResults(results, result, paths, remaining - value, nextMaxIndex);
52+
result.Pop();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func combinationSum(candidates []int, target int) [][]int {
2+
var ans [][]int
3+
4+
var dfs func(s, u int, t []int)
5+
dfs = func(s, u int, t []int) {
6+
if s == target {
7+
ans = append(ans, append([]int(nil), t...))
8+
return
9+
}
10+
if s > target {
11+
return
12+
}
13+
for i := u; i < len(candidates); i++ {
14+
c := candidates[i]
15+
t = append(t, c)
16+
dfs(s+c, i, t)
17+
t = t[:len(t)-1]
18+
}
19+
}
20+
21+
var t []int
22+
dfs(0, 0, t)
23+
return ans
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
private List<List<Integer>> ans;
3+
private int target;
4+
private int[] candidates;
5+
6+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
7+
ans = new ArrayList<>();
8+
this.target = target;
9+
this.candidates = candidates;
10+
dfs(0, 0, new ArrayList<>());
11+
return ans;
12+
}
13+
14+
private void dfs(int s, int u, List<Integer> t) {
15+
if (s == target) {
16+
ans.add(new ArrayList<>(t));
17+
return;
18+
}
19+
if (s > target) {
20+
return;
21+
}
22+
for (int i = u; i < candidates.length; ++i) {
23+
int c = candidates[i];
24+
t.add(c);
25+
dfs(s + c, i, t);
26+
t.remove(t.size() - 1);
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
ans = []
4+
n = len(candidates)
5+
6+
def dfs(s, u, t):
7+
if s == target:
8+
ans.append(t.copy())
9+
return
10+
if s > target:
11+
return
12+
for i in range(u, n):
13+
c = candidates[i]
14+
t.append(c)
15+
dfs(s + c, i, t)
16+
t.pop()
17+
18+
dfs(0, 0, [])
19+
return ans

0 commit comments

Comments
 (0)