[Leetcode] 797. All Paths From Source to Target 解题报告
2018-03-31 10:40
393 查看
题目:
Given a directed, acyclic graph of
node
The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
Note:
The number of nodes in the graph will be in the range
You can print different paths in any order, but you should keep the order of nodes inside one path.
思路:
典型的DFS+Backtracking的题目:我们从0号节点开始进行DFS搜索,一旦发现达到了n-1号节点,就输出。当然在遍历到中间结点的时候,还需要回溯,以便于返回所有符合条件的路径。
代码:
class Solution {
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> ret;
vector<int> line;
vector<bool> visited(graph.size(), false);
DFS(graph, visited, ret, line, 0);
return ret;
}
private:
void DFS(vector<vector<int>> &graph, vector<bool> &visited,
vector<vector<int>> &ret, vector<int> &line, int index) {
int n = graph.size();
if (index == n - 1) { // reaches to the target
ret.push_back(line);
ret.back().push_back(index);
return;
}
if (visited[index]) { // this node has already been visited
return;
}
line.push_back(index); // mark it as visited
visited[index] = true;
for (int i = 0; i < graph[index].size(); ++i) {
DFS(graph, visited, ret, line, graph[index][i]);
}
line.pop_back(); // backtracking
visited[index] = false;
}
};
Given a directed, acyclic graph of
Nnodes. Find all possible paths from node
0to
node
N-1, and return them in any order.
The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.
Example: Input: [[1,2], [3], [3], []] Output: [[0,1,3],[0,2,3]] Explanation: The graph looks like this: 0--->1 | | v v 2--->3 There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Note:
The number of nodes in the graph will be in the range
[2, 15].
You can print different paths in any order, but you should keep the order of nodes inside one path.
思路:
典型的DFS+Backtracking的题目:我们从0号节点开始进行DFS搜索,一旦发现达到了n-1号节点,就输出。当然在遍历到中间结点的时候,还需要回溯,以便于返回所有符合条件的路径。
代码:
class Solution {
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> ret;
vector<int> line;
vector<bool> visited(graph.size(), false);
DFS(graph, visited, ret, line, 0);
return ret;
}
private:
void DFS(vector<vector<int>> &graph, vector<bool> &visited,
vector<vector<int>> &ret, vector<int> &line, int index) {
int n = graph.size();
if (index == n - 1) { // reaches to the target
ret.push_back(line);
ret.back().push_back(index);
return;
}
if (visited[index]) { // this node has already been visited
return;
}
line.push_back(index); // mark it as visited
visited[index] = true;
for (int i = 0; i < graph[index].size(); ++i) {
DFS(graph, visited, ret, line, graph[index][i]);
}
line.pop_back(); // backtracking
visited[index] = false;
}
};
相关文章推荐
- LeetCode Merge k Sorted Lists 解题报告
- Leetcode #60. Permutation Sequence 排列组合序列 解题报告
- LeetCode-Kth Smallest Element in a BST-解题报告
- 【LeetCode】Contains Duplicate 解题报告
- 【LeetCode】Reverse Linked List 解题报告(Python & java)
- 【LeetCode】98. Validate Binary Search Tree(Medium)解题报告
- LeetCode-Excel Sheet Column Title-解题报告
- Leetcode 103. Binary Tree Zigzag Level Order Traversal 二叉树Zigzag遍历 解题报告
- 【LeetCode】Course Schedule II 解题报告
- [Leetcode] 402. Remove K Digits 解题报告
- LeetCode: Reverse Integer 解题报告
- LeetCode 191.Number of 1 Bits 解题报告
- [LeetCode]Palindrome Partitioning,解题报告
- [Leetcode] 53. Maximum Subarray 解题报告
- Leetcode 解题报告
- 【LeetCode】27.Remove Element(Easy)解题报告
- 【LeetCode】Pascal's Triangle & II 解题报告
- Leetcode 448. Find All Numbers Disappeared in an Array 找没有的数 解题报告
- [leetcode] 251. Flatten 2D Vector 解题报告
- 【LeetCode】226.Invert Binary Tree(Easy)解题报告