LeetCode797. All Paths From Source to Target

news/2024/7/20 20:09:23 标签: 深度优先, 算法, 数据结构, leetcode, c++

文章目录

    • 一、题目
    • 二、题解

一、题目

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).

Example 1:

Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Example 2:

Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]

Constraints:

n == graph.length
2 <= n <= 15
0 <= graph[i][j] < n
graph[i][j] != i (i.e., there will be no self-loops).
All the elements of graph[i] are unique.
The input graph is guaranteed to be a DAG.

二、题解

class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    void dfs(vector<vector<int>>& graph,int x){
        if(x == graph.size() - 1){
            res.push_back(path);
            return;
        }
        for(int i = 0;i < graph[x].size();i++){
            path.push_back(graph[x][i]);
            dfs(graph,graph[x][i]);
            path.pop_back();
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        path.push_back(0);
        dfs(graph,0);
        return res;
    }
};

http://www.niftyadmin.cn/n/5227943.html

相关文章

《opencv实用探索·五》opencv小白也能看懂的图像腐蚀

1、图像腐蚀原理简单理解&#xff1a; 腐蚀是形态学最基本的操作&#xff0c;都是针对白色部分&#xff08;高亮部分&#xff09;而言的。即原图像中高亮部分被蚕食&#xff0c;得到比原图更小的区域。 2、图像腐蚀的作用&#xff1a; &#xff08;1&#xff09;去掉毛刺&…

pip安装、更新、卸载

目录 一、安装pip1. ensurepip2. get-pip.py2.1 下载get-pip.py2.2 pip安装2.3 检查pip版本 二、更新pip三、卸载pip 参考https://pip.pypa.io/en/stable/installation/ 一、安装pip 1. ensurepip python -m ensurepip --upgrade2. get-pip.py 2.1 下载get-pip.py 终端执行…

力扣labuladong——一刷day61

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、力扣865. 具有所有最深节点的最小子树二、力扣1123. 最深叶节点的最近公共祖先三、力扣1026. 节点与其祖先之间的最大差值四、力扣1120. 子树的最大平均值 …

golang 集成logrus日志框架

1、安装 go get github.com/sirupsen/logrus 实现日志滚动 go get gopkg.in/natefinch/lumberjack.v2 2、初始化logrus参数 var Logger = logrus.New()func SetLogrus(logConf conf.LogConfig) {Logger.SetLevel(GetLevel(logConf.Level))Logger.SetReportCaller(true)Logg…

代码随想录算法训练营第23天|● 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树 ● 总结篇

108. 将有序数组转换为二叉搜索树 简单 给你一个整数数组 nums &#xff0c;其中元素已经按 升序 排列&#xff0c;请你将其转换为一棵 高度平衡 二叉搜索树。 高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。 示例 1&#xff1a; …

【开题报告】基于SpringBoot的婚纱店试妆预约平台的设计与实现

1.选题背景 婚礼是人生中的重要时刻&#xff0c;而试妆是婚礼准备过程中不可或缺的一环。传统的婚纱店试妆预约方式通常需要亲自到店或通过电话预约&#xff0c;这样的方式可能存在一些问题。首先&#xff0c;用户需要花费时间和精力到店进行预约&#xff0c;对于忙碌的现代人…

Netty Review - 探索Channel和Pipeline的内部机制

文章目录 概念Channel Pipeline实现原理分析详解 Inbound事件和Outbound事件演示Code 概念 Netty中的Channel和Pipeline是其核心概念&#xff0c;它们在构建高性能网络应用程序时起着重要作用。 Channel&#xff1a; 在Netty中&#xff0c;Channel表示一个开放的连接&#xff…

vue多选框 某些状态下禁止选择

在做vue多选框的时候&#xff0c;禁止多选&#xff0c;当时想都没想直接在computed里面把row-selection 直接当成方法写在里面了&#xff0c;但是后来发现一些状态不能用&#xff0c;比如清楚多选&#xff0c;selectedRowKeys没有效果&#xff0c;这里记录一下 // 最开始的代码…