diff --git a/solution/0700-0799/0797.All Paths From Source to Target/README.md b/solution/0700-0799/0797.All Paths From Source to Target/README.md index f8d406b5fa3e5..4deb1aa603807 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/README.md +++ b/solution/0700-0799/0797.All Paths From Source to Target/README.md @@ -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; +} +``` + diff --git a/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md b/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md index 953e36379cc03..63686492a1eb3 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md +++ b/solution/0700-0799/0797.All Paths From Source to Target/README_EN.md @@ -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; +} +``` + diff --git a/solution/0700-0799/0797.All Paths From Source to Target/Solution.ts b/solution/0700-0799/0797.All Paths From Source to Target/Solution.ts new file mode 100644 index 0000000000000..0e3c548144321 --- /dev/null +++ b/solution/0700-0799/0797.All Paths From Source to Target/Solution.ts @@ -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; +}