File tree 5 files changed +15
-25
lines changed
0000-0099/0039.Combination Sum
0700-0799/0797.All Paths From Source to Target
5 files changed +15
-25
lines changed Original file line number Diff line number Diff line change 65
65
``` python
66
66
class Solution :
67
67
def combinationSum (self , candidates : List[int ], target : int ) -> List[List[int ]]:
68
- ans = []
69
- n = len (candidates)
70
-
71
68
def dfs (s , u , t ):
72
69
if s == target:
73
- ans.append(t.copy() )
70
+ ans.append(t[:] )
74
71
return
75
72
if s > target:
76
73
return
77
- for i in range (u, n ):
74
+ for i in range (u, len (candidates) ):
78
75
c = candidates[i]
79
76
t.append(c)
80
77
dfs(s + c, i, t)
81
78
t.pop()
82
79
80
+ ans = []
83
81
dfs(0 , 0 , [])
84
82
return ans
85
83
```
Original file line number Diff line number Diff line change 71
71
``` python
72
72
class Solution :
73
73
def combinationSum (self , candidates : List[int ], target : int ) -> List[List[int ]]:
74
- ans = []
75
- n = len (candidates)
76
-
77
74
def dfs (s , u , t ):
78
75
if s == target:
79
- ans.append(t.copy() )
76
+ ans.append(t[:] )
80
77
return
81
78
if s > target:
82
79
return
83
- for i in range (u, n ):
80
+ for i in range (u, len (candidates) ):
84
81
c = candidates[i]
85
82
t.append(c)
86
83
dfs(s + c, i, t)
87
84
t.pop()
88
85
86
+ ans = []
89
87
dfs(0 , 0 , [])
90
88
return ans
91
89
```
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
3
- ans = []
4
- n = len (candidates )
5
-
6
3
def dfs (s , u , t ):
7
4
if s == target :
8
- ans .append (t . copy () )
5
+ ans .append (t [:] )
9
6
return
10
7
if s > target :
11
8
return
12
- for i in range (u , n ):
9
+ for i in range (u , len ( candidates ) ):
13
10
c = candidates [i ]
14
11
t .append (c )
15
12
dfs (s + c , i , t )
16
13
t .pop ()
17
14
15
+ ans = []
18
16
dfs (0 , 0 , [])
19
17
return ans
Original file line number Diff line number Diff line change 92
92
``` python
93
93
class Solution :
94
94
def allPathsSourceTarget (self , graph : List[List[int ]]) -> List[List[int ]]:
95
- ans = []
96
-
97
95
def dfs (t ):
98
96
if t[- 1 ] == len (graph) - 1 :
99
- ans.append(t.copy() )
97
+ ans.append(t[:] )
100
98
return
101
-
102
99
for v in graph[t[- 1 ]]:
103
100
t.append(v)
104
101
dfs(t)
105
102
t.pop()
106
-
103
+
104
+ ans = []
107
105
dfs([0 ])
108
106
return ans
109
107
```
Original file line number Diff line number Diff line change 88
88
``` python
89
89
class Solution :
90
90
def allPathsSourceTarget (self , graph : List[List[int ]]) -> List[List[int ]]:
91
- ans = []
92
-
93
91
def dfs (t ):
94
92
if t[- 1 ] == len (graph) - 1 :
95
- ans.append(t.copy() )
93
+ ans.append(t[:] )
96
94
return
97
-
98
95
for v in graph[t[- 1 ]]:
99
96
t.append(v)
100
97
dfs(t)
101
98
t.pop()
102
-
99
+
100
+ ans = []
103
101
dfs([0 ])
104
102
return ans
105
103
```
You can’t perform that action at this time.
0 commit comments