Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions solution/0700-0799/0797.All Paths From Source to Target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ var allPathsSourceTarget = function (graph) {
};
```

#### TypeScript

```ts
function allPathsSourceTarget(graph: number[][]): number[][] {
const ans: number[][] = [];

const dfs = (path: number[]) => {
const curr = path.at(-1)!;
if (curr === graph.length - 1) {
ans.push([...path]);
return;
}

for (const v of graph[curr]) {
path.push(v);
dfs(path);
path.pop();
}
};

dfs([0]);

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,32 @@ var allPathsSourceTarget = function (graph) {
};
```

#### TypeScript

```ts
function allPathsSourceTarget(graph: number[][]): number[][] {
const ans: number[][] = [];

const dfs = (path: number[]) => {
const curr = path.at(-1)!;
if (curr === graph.length - 1) {
ans.push([...path]);
return;
}

for (const v of graph[curr]) {
path.push(v);
dfs(path);
path.pop();
}
};

dfs([0]);

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function allPathsSourceTarget(graph: number[][]): number[][] {
const ans: number[][] = [];

const dfs = (path: number[]) => {
const curr = path.at(-1)!;
if (curr === graph.length - 1) {
ans.push([...path]);
return;
}

for (const v of graph[curr]) {
path.push(v);
dfs(path);
path.pop();
}
};

dfs([0]);

return ans;
}