Skip to content

Commit 827ba47

Browse files
authored
feat: add ts solution to lc problem: No.0797 (doocs#3214)
1 parent ca04b87 commit 827ba47

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,32 @@ var allPathsSourceTarget = function (graph) {
223223
};
224224
```
225225

226+
#### TypeScript
227+
228+
```ts
229+
function allPathsSourceTarget(graph: number[][]): number[][] {
230+
const ans: number[][] = [];
231+
232+
const dfs = (path: number[]) => {
233+
const curr = path.at(-1)!;
234+
if (curr === graph.length - 1) {
235+
ans.push([...path]);
236+
return;
237+
}
238+
239+
for (const v of graph[curr]) {
240+
path.push(v);
241+
dfs(path);
242+
path.pop();
243+
}
244+
};
245+
246+
dfs([0]);
247+
248+
return ans;
249+
}
250+
```
251+
226252
<!-- tabs:end -->
227253

228254
<!-- solution:end -->

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

+26
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,32 @@ var allPathsSourceTarget = function (graph) {
215215
};
216216
```
217217

218+
#### TypeScript
219+
220+
```ts
221+
function allPathsSourceTarget(graph: number[][]): number[][] {
222+
const ans: number[][] = [];
223+
224+
const dfs = (path: number[]) => {
225+
const curr = path.at(-1)!;
226+
if (curr === graph.length - 1) {
227+
ans.push([...path]);
228+
return;
229+
}
230+
231+
for (const v of graph[curr]) {
232+
path.push(v);
233+
dfs(path);
234+
path.pop();
235+
}
236+
};
237+
238+
dfs([0]);
239+
240+
return ans;
241+
}
242+
```
243+
218244
<!-- tabs:end -->
219245

220246
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function allPathsSourceTarget(graph: number[][]): number[][] {
2+
const ans: number[][] = [];
3+
4+
const dfs = (path: number[]) => {
5+
const curr = path.at(-1)!;
6+
if (curr === graph.length - 1) {
7+
ans.push([...path]);
8+
return;
9+
}
10+
11+
for (const v of graph[curr]) {
12+
path.push(v);
13+
dfs(path);
14+
path.pop();
15+
}
16+
};
17+
18+
dfs([0]);
19+
20+
return ans;
21+
}

0 commit comments

Comments
 (0)