Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6febd63

Browse files
committed
feat: add solutions to lc problem: No.0797
No.0797. All Paths From Source to Target`
1 parent 0258364 commit 6febd63

File tree

3 files changed

+122
-36
lines changed

3 files changed

+122
-36
lines changed

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ class Solution:
9494
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
9595
ans = []
9696

97-
def dfs(i, path):
98-
if i == len(graph) - 1:
99-
ans.append(path.copy())
97+
def dfs(t):
98+
if t[-1] == len(graph) - 1:
99+
ans.append(t.copy())
100100
return
101-
for j in graph[i]:
102-
path.append(j)
103-
dfs(j, path)
104-
path.pop(-1)
101+
102+
for v in graph[t[-1]]:
103+
t.append(v)
104+
dfs(t)
105+
t.pop()
105106

106-
dfs(0, [0])
107+
dfs([0])
107108
return ans
108109
```
109110

@@ -148,21 +149,22 @@ class Solution {
148149
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
149150
ans = new ArrayList<>();
150151
this.graph = graph;
151-
List<Integer> path = new ArrayList<>();
152-
path.add(0);
153-
dfs(0, path);
152+
List<Integer> t = new ArrayList<>();
153+
t.add(0);
154+
dfs(t);
154155
return ans;
155156
}
156157

157-
private void dfs(int i, List<Integer> path) {
158-
if (i == graph.length - 1) {
159-
ans.add(new ArrayList<>(path));
158+
private void dfs(List<Integer> t) {
159+
int cur = t.get(t.size() - 1);
160+
if (cur == graph.length - 1) {
161+
ans.add(new ArrayList<>(t));
160162
return;
161163
}
162-
for (int j : graph[i]) {
163-
path.add(j);
164-
dfs(j, path);
165-
path.remove(path.size() - 1);
164+
for (int v : graph[cur]) {
165+
t.add(v);
166+
dfs(t);
167+
t.remove(t.size() - 1);
166168
}
167169
}
168170
}
@@ -230,6 +232,35 @@ func allPathsSourceTarget(graph [][]int) [][]int {
230232
}
231233
```
232234

235+
### **JavaScript**
236+
237+
```js
238+
/**
239+
* @param {number[][]} graph
240+
* @return {number[][]}
241+
*/
242+
var allPathsSourceTarget = function(graph) {
243+
const ans = [];
244+
const t = [0];
245+
246+
const dfs = t => {
247+
const cur = t[t.length - 1];
248+
if (cur == graph.length - 1) {
249+
ans.push([...t]);
250+
return;
251+
}
252+
for (const v of graph[cur]) {
253+
t.push(v);
254+
dfs(t);
255+
t.pop();
256+
}
257+
}
258+
259+
dfs(t);
260+
return ans;
261+
};
262+
```
263+
233264
### **...**
234265

235266
```

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ class Solution:
9090
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
9191
ans = []
9292

93-
def dfs(i, path):
94-
if i == len(graph) - 1:
95-
ans.append(path.copy())
93+
def dfs(t):
94+
if t[-1] == len(graph) - 1:
95+
ans.append(t.copy())
9696
return
97-
for j in graph[i]:
98-
path.append(j)
99-
dfs(j, path)
100-
path.pop(-1)
97+
98+
for v in graph[t[-1]]:
99+
t.append(v)
100+
dfs(t)
101+
t.pop()
101102

102-
dfs(0, [0])
103+
dfs([0])
103104
return ans
104105
```
105106

@@ -142,21 +143,22 @@ class Solution {
142143
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
143144
ans = new ArrayList<>();
144145
this.graph = graph;
145-
List<Integer> path = new ArrayList<>();
146-
path.add(0);
147-
dfs(0, path);
146+
List<Integer> t = new ArrayList<>();
147+
t.add(0);
148+
dfs(t);
148149
return ans;
149150
}
150151

151-
private void dfs(int i, List<Integer> path) {
152-
if (i == graph.length - 1) {
153-
ans.add(new ArrayList<>(path));
152+
private void dfs(List<Integer> t) {
153+
int cur = t.get(t.size() - 1);
154+
if (cur == graph.length - 1) {
155+
ans.add(new ArrayList<>(t));
154156
return;
155157
}
156-
for (int j : graph[i]) {
157-
path.add(j);
158-
dfs(j, path);
159-
path.remove(path.size() - 1);
158+
for (int v : graph[cur]) {
159+
t.add(v);
160+
dfs(t);
161+
t.remove(t.size() - 1);
160162
}
161163
}
162164
}
@@ -224,6 +226,35 @@ func allPathsSourceTarget(graph [][]int) [][]int {
224226
}
225227
```
226228

229+
### **JavaScript**
230+
231+
```js
232+
/**
233+
* @param {number[][]} graph
234+
* @return {number[][]}
235+
*/
236+
var allPathsSourceTarget = function(graph) {
237+
const ans = [];
238+
const t = [0];
239+
240+
const dfs = t => {
241+
const cur = t[t.length - 1];
242+
if (cur == graph.length - 1) {
243+
ans.push([...t]);
244+
return;
245+
}
246+
for (const v of graph[cur]) {
247+
t.push(v);
248+
dfs(t);
249+
t.pop();
250+
}
251+
}
252+
253+
dfs(t);
254+
return ans;
255+
};
256+
```
257+
227258
### **...**
228259

229260
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[][]} graph
3+
* @return {number[][]}
4+
*/
5+
var allPathsSourceTarget = function(graph) {
6+
const ans = [];
7+
const t = [0];
8+
9+
const dfs = t => {
10+
const cur = t[t.length - 1];
11+
if (cur == graph.length - 1) {
12+
ans.push([...t]);
13+
return;
14+
}
15+
for (const v of graph[cur]) {
16+
t.push(v);
17+
dfs(t);
18+
t.pop();
19+
}
20+
}
21+
22+
dfs(t);
23+
return ans;
24+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /