From 61db8479774f50ea42edc7b3145066e3f3e08998 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 7 Jul 2024 16:01:12 +0300 Subject: [PATCH] feat: add ts solution to lc problem: No.0797 --- .../README.md | 26 +++++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++++ .../Solution.ts | 21 +++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 solution/0700-0799/0797.All Paths From Source to Target/Solution.ts 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; +}