Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to 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.

问0->n-1有几条路径

其实没什么,就是看输入有点不懂怎么建图,把图建立好,我们dfs就好了

class Solution {
public:
int Mp[][];
vector<vector<int>>ans;
vector<int>tmp;
void dfs(int n,int len){
if(len-==n){
ans.push_back(tmp);
return;
}
for(int i=;i<len;i++){
if(Mp[n][i]){
tmp.push_back(i);
dfs(i,len);
tmp.pop_back();
}
}
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
int len=graph.size();
for(int i=;i<len;i++){
for(int j=;j<graph[i].size();j++){
Mp[i][graph[i][j]]=;
}
}
tmp.clear();
ans.clear();
tmp.push_back();
dfs(,len);
return ans; }
};

75th LeetCode Weekly Contest All Paths From Source to Target的更多相关文章

  1. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  2. 75th LeetCode Weekly Contest Smallest Rotation with Highest Score

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...

  3. 75th LeetCode Weekly Contest Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  4. 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  5. 【leetcode】797. All Paths From Source to Target

    Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths fro ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

随机推荐

  1. 第一天:tomcat相关知识和浏览器的访问机制

    1.tomcat的目录结构 1)bin目录:启动和关闭tomcat以及其他的脚本命令  2)conf目录:存放各种配置文件 a.server.xml配置文件的配置: *<host/>标签: ...

  2. IE双边距bug

    标准参考 根据 W3C CSS2.1 规范中的描述,对于非替换的浮动元素,若 'margin-left' 或 'margin-right' 特性的计算值为 'auto',则它们的实际使用值为 '0'. ...

  3. 使用ffmpeg合并视频

    命令: ffmpeg -i concat:"1.avi|2.avi" -vcodec copy -acodec copy "3.avi" ffmpeg下载:ht ...

  4. Solr之缓存篇

    原文出自:http://my.oschina.net/u/1026644/blog/123957 Solr在Lucene之上开发了很多Cache功能,从目前提供的Cache类型有: (1)filter ...

  5. Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识

    网站站点的背景调研 1. 检查 robots.txt 网站都会定义robots.txt 文件,这个文件就是给 网络爬虫 来了解爬取该网站时存在哪些限制.当然了,这个限制仅仅只是一个建议,你可以遵守,也 ...

  6. hihocoder1513 小Hi的烦恼

    传送门 分析 论bitset的妙用......我们利用桶排将输入的数据排序,之后分别考虑5维,a[i][j]表示考虑第i个人第j维的情况下于其它人的大小关系.最后将5维的信息并起来求1的个数即可 代码 ...

  7. Java/C++中数组的区别

    1. 数组名区别 -------------------------------------- 1. java中不用说,本着一切皆对象的原则,所以java中的数组也是对象.那么数组类是哪个,当然不是j ...

  8. Bulma 源码解析之 .columns 类

    {说明} 这一部分的源码内容被我简化了,另外我还额外添加了一个辅助类 is-grow. .columns // 修饰类 &.is-centered justify-content: cente ...

  9. 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(四)

    ViewPagerIndicator+ViewPager 要想使用ViewPagerIndicator,要使用到viewPagerlibrary开源库 top.xml <?xml version ...

  10. c#操作word类,进行html和word文档的互相转换

    实例引用:http://www.7es.cn/Software_development/171.shtml using Microsoft.Office.Core;using Word = Micro ...