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. ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?(图文详解)

    不多说,直接上干货! 问题详情 ubuntu系统里vi编辑器时,按方向箭头输入是乱码的ABCD字母?  解决办法 是由于预装的vim软件没更新,运行   sudo apt-get install vi ...

  2. hadoop再次集群搭建(4)-Cloudera Manager Installation

       决定选择 Cloudera Manager 进行安装,阅读官方文档,掌握大概脉络.         Cloudera Manager在集群已经实现ssh免秘钥登录,能够访问网络资源和本地资源的情 ...

  3. 数据库监听。数据库一次notify,Activity多次接收

    今天项目中发现一个bug: (1)当uri数据库中有更新,会从数据库层DataService中通知应用层,调用notifyChange: mContext.getContentResolver().n ...

  4. ionic 页面乱码

    是文本编辑器的问题.文本编辑器保存时默认保存成Encoding:ANSI编码格式,保存成utf-8就好了

  5. C++面向对象类的实例题目六

    问题描述: 编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数. 程序代码: #include<iostream> ...

  6. Qt中显示图像的两种方法

    博客转载自:https://blog.csdn.net/lg1259156776/article/details/52325361 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不 ...

  7. GCD 学习(三)Main&Global Dispatch Queue

    摘录自:http://zhuyanfeng.com/archives/3066 Main Dispatch Queue是在主线程中执行任务的Dispatch Queue.因为主线程只有1个,所以Mai ...

  8. Ubuntu相关IP配置(转)

    配置文件:/etc/network/interfaces 打开后里面可设置DHCP或手动设置静态ip.前面auto eth0,让网卡开机自动挂载. 1. 以DHCP方式配置网卡 编辑文件/etc/ne ...

  9. ubuntu16.04.2安装tftp服务器

    1.安装 $ apt-get install tftp-hpa tftpd-hpa tftp-hpa是客户端 tftpd-hpa是服务器 2.创建目录 $ mkdir /tftpboot 这是创建tf ...

  10. web.xml 注册中央调度器Url-pattern 要注意的地方(五)

    一.url-pattern 里面不能用/*     二.最好不要写成杠,如果使用“/”,中央调度器会把静态资源拦截掉,比如图片会不显示,或者css不能用.   但,如果地址栏传参数的时候比如是/del ...