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.

  1. Example:
  2. Input: [[1,2], [3], [3], []]
  3. Output: [[0,1,3],[0,2,3]]
  4. Explanation: The graph looks like this:
  5. 0--->1
  6. | |
  7. v v
  8. 2--->3
  9. 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就好了

  1. class Solution {
  2. public:
  3. int Mp[][];
  4. vector<vector<int>>ans;
  5. vector<int>tmp;
  6. void dfs(int n,int len){
  7. if(len-==n){
  8. ans.push_back(tmp);
  9. return;
  10. }
  11. for(int i=;i<len;i++){
  12. if(Mp[n][i]){
  13. tmp.push_back(i);
  14. dfs(i,len);
  15. tmp.pop_back();
  16. }
  17. }
  18. }
  19. vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
  20. int len=graph.size();
  21. for(int i=;i<len;i++){
  22. for(int j=;j<graph[i].size();j++){
  23. Mp[i][graph[i][j]]=;
  24. }
  25. }
  26. tmp.clear();
  27. ans.clear();
  28. tmp.push_back();
  29. dfs(,len);
  30. return ans;
  31.  
  32. }
  33. };

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. 关于android中,菜单按钮点击事件首次执行之后再次执行需要双击按钮的问题

    有时候在获取事件的时候,需要双击才能获取,解决方法很简单,把返回值设为true,那么这个事件就不会再分发了,我预计是设为其他值会继续分发,造成事件的相应混乱

  2. 使用线程池优化Echo模型

    在上一篇文章中 http://www.cnblogs.com/gosaint/p/8492356.html         我阐述了使用线程为每一个客户端创建一个工作线程来负责任务的执行.但是会存在如 ...

  3. Apache的DBUtils框架学习(转)

    原文地址:http://www.cnblogs.com/xdp-gacl/p/4007225.html 一.commons-dbutils简介 commons-dbutils 是 Apache 组织提 ...

  4. ROS Learning-015 learning_tf(编程) 编写一个监听器程序 (Python版)

    ROS Indigo learning_tf-02 编写一个 监听器 程序 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 1 ...

  5. SDUT 3403 数据结构实验之排序六:希尔排序

    数据结构实验之排序六:希尔排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 我们已经学习 ...

  6. wrodcount

    PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划  20  20 · Estimate · 估计这个任务需要多少时间 20   20 Development 开 ...

  7. wordCount总结

    1.github地址:https://github.com/husterSyy/SoftTest 2.PSP表格   psp 2.1 psp阶段 预估耗时(分钟) 实际耗时(分钟) Planning ...

  8. Bootstrap 组件之 Nav

    一.简介 Nav 指导航页.这里 是一个线上例子. 使用了 .nav 的标签就是一个 Nav.下面举例. {注意} 记住,下面的几种导航页都依赖 .nav. 二.导航页 添加 .nav-tabs. & ...

  9. 二维码的生成细节和原理 -- 转http://news.cnblogs.com/n/191671/

    二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...

  10. python产生随机字符串

    def GenerateRandomString(len, basechars = []): if (basechars == []): x = range(ord() x.extend(range( ...