Skip to content

Commit 499fd5f

Browse files
committed
feat: add solutions to lc problem: No.0039
No.0039.Combination Sum
1 parent 8b74f92 commit 499fd5f

File tree

5 files changed

+15
-25
lines changed

5 files changed

+15
-25
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,19 @@ DFS。
6565
```python
6666
class Solution:
6767
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
68-
ans = []
69-
n = len(candidates)
70-
7168
def dfs(s, u, t):
7269
if s == target:
73-
ans.append(t.copy())
70+
ans.append(t[:])
7471
return
7572
if s > target:
7673
return
77-
for i in range(u, n):
74+
for i in range(u, len(candidates)):
7875
c = candidates[i]
7976
t.append(c)
8077
dfs(s + c, i, t)
8178
t.pop()
8279

80+
ans = []
8381
dfs(0, 0, [])
8482
return ans
8583
```

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,19 @@ DFS.
7171
```python
7272
class Solution:
7373
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
74-
ans = []
75-
n = len(candidates)
76-
7774
def dfs(s, u, t):
7875
if s == target:
79-
ans.append(t.copy())
76+
ans.append(t[:])
8077
return
8178
if s > target:
8279
return
83-
for i in range(u, n):
80+
for i in range(u, len(candidates)):
8481
c = candidates[i]
8582
t.append(c)
8683
dfs(s + c, i, t)
8784
t.pop()
8885

86+
ans = []
8987
dfs(0, 0, [])
9088
return ans
9189
```
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution:
22
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3-
ans = []
4-
n = len(candidates)
5-
63
def dfs(s, u, t):
74
if s == target:
8-
ans.append(t.copy())
5+
ans.append(t[:])
96
return
107
if s > target:
118
return
12-
for i in range(u, n):
9+
for i in range(u, len(candidates)):
1310
c = candidates[i]
1411
t.append(c)
1512
dfs(s + c, i, t)
1613
t.pop()
1714

15+
ans = []
1816
dfs(0, 0, [])
1917
return ans

solution/0700-0799/0797.All Paths From Source to Target/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,16 @@ DFS:
9292
```python
9393
class Solution:
9494
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
95-
ans = []
96-
9795
def dfs(t):
9896
if t[-1] == len(graph) - 1:
99-
ans.append(t.copy())
97+
ans.append(t[:])
10098
return
101-
10299
for v in graph[t[-1]]:
103100
t.append(v)
104101
dfs(t)
105102
t.pop()
106-
103+
104+
ans = []
107105
dfs([0])
108106
return ans
109107
```

solution/0700-0799/0797.All Paths From Source to Target/README_EN.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,16 @@ DFS:
8888
```python
8989
class Solution:
9090
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
91-
ans = []
92-
9391
def dfs(t):
9492
if t[-1] == len(graph) - 1:
95-
ans.append(t.copy())
93+
ans.append(t[:])
9694
return
97-
9895
for v in graph[t[-1]]:
9996
t.append(v)
10097
dfs(t)
10198
t.pop()
102-
99+
100+
ans = []
103101
dfs([0])
104102
return ans
105103
```

0 commit comments

Comments
 (0)