Skip to content

Commit aeb97c1

Browse files
committed
feat: add solutions to lc problem: No.0039
No.0039.Combination Sum
1 parent 41ca792 commit aeb97c1

File tree

9 files changed

+667
-289
lines changed

9 files changed

+667
-289
lines changed

solution/0000-0099/0039.Combination Sum/README.md

+291-77
Large diffs are not rendered by default.

solution/0000-0099/0039.Combination Sum/README_EN.md

+279-75
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
class Solution {
22
public:
3-
vector<vector<int>> ans;
4-
vector<int> candidates;
5-
int target;
6-
73
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
8-
this->candidates = candidates;
9-
this->target = target;
4+
sort(candidates.begin(), candidates.end());
5+
vector<vector<int>> ans;
106
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-
ans.push_back(t);
18-
return;
19-
}
20-
if (s > target) return;
21-
for (int i = u; i < candidates.size(); ++i) {
22-
int c = candidates[i];
23-
t.push_back(c);
24-
dfs(s + c, i, t);
7+
function<void(int, int)> dfs = [&](int i, int s) {
8+
if (s == 0) {
9+
ans.emplace_back(t);
10+
return;
11+
}
12+
if (i >= candidates.size() || s < candidates[i]) {
13+
return;
14+
}
15+
dfs(i + 1, s);
16+
t.push_back(candidates[i]);
17+
dfs(i, s - candidates[i]);
2518
t.pop_back();
26-
}
19+
};
20+
dfs(0, target);
21+
return ans;
2722
}
2823
};
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,26 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
public class Solution {
2+
private List<IList<int>> ans = new List<IList<int>>();
3+
private List<int> t = new List<int>();
4+
private int[] candidates;
45

5-
public class Solution
6-
{
7-
public IList<IList<int>> CombinationSum(int[] candidates, int target)
8-
{
6+
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
97
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;
8+
this.candidates = candidates;
9+
dfs(0, target);
10+
return ans;
3211
}
3312

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));
13+
private void dfs(int i, int s) {
14+
if (s == 0) {
15+
ans.Add(new List<int>(t));
4016
return;
4117
}
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();
18+
if (i >= candidates.Length || s < candidates[i]) {
19+
return;
5320
}
21+
dfs(i + 1, s);
22+
t.Add(candidates[i]);
23+
dfs(i, s - candidates[i]);
24+
t.RemoveAt(t.Count - 1);
5425
}
5526
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
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...))
1+
func combinationSum(candidates []int, target int) (ans [][]int) {
2+
sort.Ints(candidates)
3+
t := []int{}
4+
var dfs func(i, s int)
5+
dfs = func(i, s int) {
6+
if s == 0 {
7+
cp := make([]int, len(t))
8+
copy(cp, t)
9+
ans = append(ans, cp)
810
return
911
}
10-
if s > target {
12+
if i >= len(candidates) || s < candidates[i] {
1113
return
1214
}
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-
}
15+
dfs(i+1, s)
16+
t = append(t, candidates[i])
17+
dfs(i, s-candidates[i])
18+
t = t[:len(t)-1]
1919
}
20-
21-
var t []int
22-
dfs(0, 0, t)
23-
return ans
20+
dfs(0, target)
21+
return
2422
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
class Solution {
2-
private List<List<Integer>> ans;
3-
private int target;
2+
private List<List<Integer>> ans = new ArrayList<>();
3+
private List<Integer> t = new ArrayList<>();
44
private int[] candidates;
55

66
public List<List<Integer>> combinationSum(int[] candidates, int target) {
7-
ans = new ArrayList<>();
8-
this.target = target;
7+
Arrays.sort(candidates);
98
this.candidates = candidates;
10-
dfs(0, 0, new ArrayList<>());
9+
dfs(0, target);
1110
return ans;
1211
}
1312

14-
private void dfs(int s, int u, List<Integer> t) {
15-
if (s == target) {
16-
ans.add(new ArrayList<>(t));
13+
private void dfs(int i, int s) {
14+
if (s == 0) {
15+
ans.add(new ArrayList(t));
1716
return;
1817
}
19-
if (s > target) {
18+
if (i >= candidates.length || s < candidates[i]) {
2019
return;
2120
}
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-
}
21+
dfs(i + 1, s);
22+
t.add(candidates[i]);
23+
dfs(i, s - candidates[i]);
24+
t.remove(t.size() - 1);
2825
}
2926
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution:
22
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3-
def dfs(s, u, t):
4-
if s == target:
3+
def dfs(i: int, s: int):
4+
if s == 0:
55
ans.append(t[:])
66
return
7-
if s > target:
7+
if i >= len(candidates) or s < candidates[i]:
88
return
9-
for i in range(u, len(candidates)):
10-
c = candidates[i]
11-
t.append(c)
12-
dfs(s + c, i, t)
13-
t.pop()
9+
dfs(i + 1, s)
10+
t.append(candidates[i])
11+
dfs(i, s - candidates[i])
12+
t.pop()
1413

14+
candidates.sort()
15+
t = []
1516
ans = []
16-
dfs(0, 0, [])
17+
dfs(0, target)
1718
return ans
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
impl Solution {
2-
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3-
if count < 0 {
2+
fn dfs(i: usize, s: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>) {
3+
if s == 0 {
4+
ans.push(t.clone());
45
return;
56
}
6-
if count == 0 {
7-
res.push(t.clone());
7+
if i >= candidates.len() || s < candidates[i] {
88
return;
99
}
10-
for j in i..candidates.len() {
11-
let num = candidates[j];
12-
t.push(num);
13-
Self::dfs(j, count - num, candidates, t, res);
14-
t.pop();
15-
}
10+
Self::dfs(i + 1, s, candidates, t, ans);
11+
t.push(candidates[i]);
12+
Self::dfs(i, s - candidates[i], candidates, t, ans);
13+
t.pop();
1614
}
1715

18-
pub fn combination_count(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
19-
let mut res = Vec::new();
20-
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
21-
res
16+
pub fn combination_sum(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
17+
candidates.sort();
18+
let mut ans = Vec::new();
19+
Self::dfs(0, target, &candidates, &mut vec![], &mut ans);
20+
ans
2221
}
23-
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
function combinationSum(candidates: number[], target: number): number[][] {
2-
const n = candidates.length;
2+
candidates.sort((a, b) => a - b);
3+
const ans: number[][] = [];
34
const t: number[] = [];
4-
const res: number[][] = [];
5-
const dfs = (i: number, sum: number) => {
6-
if (sum > target) {
5+
const dfs = (i: number, s: number) => {
6+
if (s === 0) {
7+
ans.push(t.slice());
78
return;
89
}
9-
if (sum === target) {
10-
res.push([...t]);
10+
if (i >= candidates.length || s < candidates[i]) {
1111
return;
1212
}
13-
for (let j = i; j < n; j++) {
14-
t.push(candidates[j]);
15-
dfs(j, sum + candidates[j]);
16-
t.pop();
17-
}
13+
dfs(i + 1, s);
14+
t.push(candidates[i]);
15+
dfs(i, s - candidates[i]);
16+
t.pop();
1817
};
19-
dfs(0, 0);
20-
return res;
18+
dfs(0, target);
19+
return ans;
2120
}

0 commit comments

Comments
 (0)