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 8c66ead

Browse files
committed
solve problem All Paths From Source To Target
1 parent 37de491 commit 8c66ead

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ All solutions will be accepted!
303303
|896|[Monotonic Array](https://leetcode-cn.com/problems/monotonic-array/description/)|[java/py/js](./algorithms/MonotonicArray)|Easy|
304304
|739|[Daily Temperatures](https://leetcode-cn.com/problems/daily-temperatures/description/)|[java/py/js](./algorithms/DailyTemperatures)|Medium|
305305
|921|[Minimum Add To Make Parentheses Valid](https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/description/)|[java/py/js](./algorithms/MinimumAddToMakeParenthesesValid)|Medium|
306+
|797|[All Paths From Source To Target](https://leetcode-cn.com/problems/all-paths-from-source-to-target/description/)|[java/py/js](./algorithms/AllPathsFromSourceToTarget)|Medium|
306307

307308
# Database
308309
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# All Paths From Source To Target
2+
We can solve this problem by Backtracking Algorithm
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
3+
List<List<Integer>> res = new ArrayList<List<Integer>>();
4+
if (graph.length > 0)
5+
backTrack(graph, 0, new ArrayList<Integer>(), res);
6+
return res;
7+
}
8+
9+
public void backTrack(int[][] graph, int i, List<Integer> temp, List<List<Integer>> res) {
10+
temp.add(i);
11+
if (i == graph.length - 1)
12+
res.add(temp);
13+
else {
14+
for (int v : graph[i])
15+
backTrack(graph, v, new ArrayList<Integer>(temp), res);
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} graph
3+
* @return {number[][]}
4+
*/
5+
var allPathsSourceTarget = function(graph) {
6+
let res = []
7+
if (graph.length > 0)
8+
backTrack(graph, 0, [], res)
9+
return res
10+
};
11+
12+
var backTrack = function (graph, i, temp, res) {
13+
temp.push(i)
14+
if (i == graph.length - 1)
15+
res.push(temp)
16+
else
17+
graph[i].forEach(v => backTrack(graph, v, temp.slice(), res))
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def allPathsSourceTarget(self, graph):
3+
"""
4+
:type graph: List[List[int]]
5+
:rtype: List[List[int]]
6+
"""
7+
res = []
8+
if len(graph) > 0:
9+
self.backtrack(graph, 0, [], res)
10+
return res
11+
12+
13+
def backtrack(self, graph, i, temp, res):
14+
temp.append(i)
15+
if i == len(graph) - 1:
16+
res.append(temp)
17+
else:
18+
for v in graph[i]:
19+
self.backtrack(graph, v, temp[:], res)

0 commit comments

Comments
(0)

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