LeetCode 797. All Paths From Source to Target

字符串面试题整理 自己整理的校招笔试面试时,主要的字符串题型。 立即下载

题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/

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.

题目解析:dfs一下,加一个访问标记。

代码如下:56ms Accepted beating 89.22%

class Solution {
public:
    int vis[20] = {0};
    void dfs(vector<vector<int>>& graph, vector<vector<int>>& results, vector<int> result, int current, int target){
        if (current == target)
        {
            results.push_back(result);
            return;
        }
        for (auto next : graph[current])
        {
            if (vis[next])
                continue;
            vis[next] = 1;
            result.push_back(next);
            dfs(graph, results, result, next, target);
            result.pop_back();
            vis[next] = 0;
        }
    }
    
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<vector<int>> results;
        vector<int> result = {0};
        vis[0] = 1;
        dfs(graph, results, result, 0, graph.size() - 1);
        return results;
    }
};

提交之后看到最快运行速度是52ms,看了下代码没怎么明白具体原因,欢迎大佬评论解释一下,参考代码贴在下面——

class GraphVertex
{
    public:
    
        GraphVertex(vector<int> friends):neighbors(friends),visit(false){}
        vector<int> neighbors;
        bool visit;
};


class Solution {
public:
    
    void dfsHelper(vector<GraphVertex>& graph,int start,int end,vector<int>& path,vector<vector<int> >& path_set)
    {
        if ( start == end)
        {
            path_set.push_back(path);
            return;
        }
        
        vector<int> neighbors=graph[start].neighbors;
        for ( int i =0; i < neighbors.size(); ++i)
        {
            if ( !graph[neighbors[i]].visit)
            {
                graph[neighbors[i]].visit=true;
                path.push_back(neighbors[i]);
                dfsHelper(graph,neighbors[i],end,path,path_set);
                path.pop_back();
                graph[neighbors[i]].visit=false;
                
            }
        }
      
    }
    
    
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        
        vector<GraphVertex> adjancency_list;
        for ( int i=0; i < graph.size() ; ++i)
        {
            adjancency_list.push_back(GraphVertex(graph[i]));
        }
        
        vector<vector<int> > set_of_paths;
        vector<int> path;
        
        path.push_back(0);
        adjancency_list[0].visit=true;
        
        dfsHelper(adjancency_list,0,graph.size()-1,path,set_of_paths);
        
        
        return set_of_paths;
        
    }
};

 

【AutoSAR】【EcuM】ECU状态管理 目录 一、EcuM架构位置 ​二、EcuM功能介绍 三、EcuM状态机 1.STARTUP阶段 2、UP状态 3、shutdown状态 4、休眠状态 5、OFF状态 一、EcuM架构位置 EcuM模块位于系统服务层,如下图红色方框里; 二、EcuM功能介绍 EcuM本身它管理着ECU在STARTUP, RUN,OFF,SLEEP,WAKEUP状态之间的相互切换。 责所有BSW的初始化以及去初始化 和ComM协作来管理网络状态,在需要时关闭ECU 管理所有的唤醒事件,并且在需要时 阅读详情

相关推荐

25个Prompt去除AI味,让AI写作更像真人

自然语言生成技术飞速发展,如今AI写作已经深度融入内容创作、营销运营和日常办公。但许多用户发现,AI生成的文本常常带着一股“AI味”——句子过于工整、过度总结、细节空洞,导致内容被一眼识破,甚至面临“AI率”过高的尴尬。其实,这种塑料感源于大模型的训练数据与对齐机制,它偏好生成结构完整、逻辑平滑的“安全文本”。要解决这一问题,核心不在于更换工具或复杂调参,而在于掌握提示词工程的底层逻辑:通过限制结构、注入细节、切换视角、打乱顺序等具体指令,引导模型生成更接近人类表达习惯的文本。本文基于实战经验,提供25个可

weixin_26824299的博客 262

All Paths From Source to Target(C++所有可能的路径)

All Paths From Source to Target(C++所有可能的路径)

coolsunxu的博客 388

图像识别火灾检测系统实战:从火焰特征到YOLO部署全解析

计算机视觉技术正深入改变传统消防的感知方式。图像识别火灾检测利用摄像头采集的视频流,通过颜色、动态和形态特征分析,实现早期火情的自动发现与定位。其核心原理在于,火焰在视觉上具有独特的颜色分布、闪烁特性和轮廓变化,借助目标检测模型如YOLO,可在复杂场景中区分真实火焰与干扰物。这项技术广泛应用于仓库、园区、隧道等大空间场所,是智慧消防的重要支撑。本文从火焰视觉特征、数据集构建、模型选型与系统架构设计出发,结合部署实践中的误报与算力优化经验,为开发者提供一套可落地的视频火焰检测方案。

weixin_42533751的博客 246

leetcode 797. All Paths From Source to Target(source到target的所有路径)

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

蓝羽飞鸟的博客 275

信息系统项目管理(三)软件工程相关知识

软件工程是指应用计算机科学、数学及管理科学等原理,以工程化的原则和方法来解决软件问题的工程,其目的是提高软件生成率、提高软件质量、降低软件成本。 一、需求分析 软件需求是指用户对新系统在功能、行为、性能、设计约束等方面的期望。 1.1 软件需求层次 软件的需求主要分为三个层次,从低到高依次是系统需求、用户需求和业务需求 1.1.1 系统需求 系统需求主要是从系统角度来说明软件需求,包括功能需求、非功能需求和设计约束 功能需求:规定开发人员必须在系统中实现的软件功能,满足业务需要 非功能需求:系统必须具备的

hello_1566的博客 5461

工程制图习题集张大庆课后习题答案解析

所以,我想向大家求助,希望能得到一些学习资料、学习方法和学习经验,帮助我更好地理解和掌握这门课程。在寻找学习资料的过程中,我发现了一款叫做资小料的APP,它里面有很多关于工程制图的学习资料,包括视频教程、习题解答和考试真题等。通过使用资小料,我逐渐掌握了工程制图的基本理论和方法,解决了很多学习中遇到的问题。在这里,我想推荐给和我一样在学习《工程制图习题集》这门课的同学们,希望大家也能从这款APP中受益,共同进步。上传者: 『Consentesdei』蒼迷穀。出版社: 清华大学出版社。

weixin_58833001的博客 927

LeetCode——797. 所有可能的路径(All Paths From Source to Target)[中等]——分析及代码(Java)

LeetCode——797. 所有可能的路径[All Paths From Source to Target][中等]——分析及代码[Java]一、题目二、分析及代码1. 深度优先搜索(1)思路(2)代码(3)结果三、其他 一、题目 给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序) 二维数组的第 i 个数组中的单元都表示有向图中 i 号节点所能到达的下一些节点,空就是没有下一个结点了。 译者注:有

江南土豆的博客 427

从轻量化到高精度:3DXML 转 STP 的工程数据升级技术指南

本文详细解析了3DXML转STP格式的工程数据升级技术,对比了两种格式在轻量化和高精度方面的特性差异,并提供了CATIA、FreeCAD及Python编程等多种专业级转换方案。通过实际案例展示了如何确保转换过程中的数据精度,适用于CAD设计、精密制造等场景,帮助工程师实现工程数据的高效升级。

weixin_29201859的博客 215

[源码和文档分享]基于C++实现的每对结点之间的最短路径(Floyd-Warshall算法)

1、实验题 每对结点之间的最短路径问题(Floyd-Warshall算法): G = ( V, E)是一个有n 个结点的有向图。补充ALL-PATHS算法,增加前驱矩阵,使得在求出结点间的最短路径长度矩阵A后,能够推导出每对结点间的最短路径。 2、设计思路 这里要求的是有向图中每一对节点之...

qq_38474647的博客 304

2022中山大学计算机技术专硕考研初试、复试经验帖

个人简介 推荐几个我自己感觉对考研非常有帮助的小助手吧。可以帮助节省时间! 考研时间规划总览 初试篇 数学 英语 政治 408 复试篇 机试 笔试 面试 关于选导师 个人感言

zik的博客 1万+

【实战教程】OpenClaw接入MySQL数据库实战指南

本文介绍了在虚拟机Ubuntu中访问宿主机Win11中MySQL数据库的配置方法,重点包括创建远程用户、权限配置和安全设置。首先在MySQL中创建具有只读权限的用户并刷新权限,然后调整服务器设置允许远程连接。文章还提供了某金融机构财富管理系统的数据库设计方案,包含理财产品表、客户表和份额表的核心结构,其中金额字段使用DECIMAL类型确保精度,并设置唯一键约束。文末强调测试环境配置完成后应恢复安全更新模式以保障生产环境安全。

海兰 8700

797. All Paths From Source to Target

/*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. ...

博客 506

高级用法:LangChain 安全防护(Guardrails)

在调用开始时校验请求,可做关键词过滤、身份校验、限流等。= "human":return {}],# 使用],= "human":return {}],

Long_像风一样自由 318

[leetcode]797. All Paths From Source to Target

[leetcode]797. All Paths From Source to Target Analysis 周四不知道快不快乐—— [有点想回家过暑假的冲动!] Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return th...

weixin_32135877的博客 301

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode 797 All Paths From Source to Target

LeetCode 797 All Paths From Source to Target Description 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 giv...

一只小羊 382

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[LeetCode] 797. All Paths From Source to Target

原题链接: https://leetcode.com/problems/all-paths-from-source-to-target/ 1. 题目介绍 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....

Ber03的博客 207

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode //C - 797. All Paths From Source to Target

This problem requires finding all possible paths from node 0 to node (n-1) in a directed acyclic graph (DAG). The solution uses Depth-First Search (DFS) to explore each path recursively. A temporary path array tracks the current traversal, and when the tar

Made in Code 1394

[leetcode] 797. All Paths From Source to Target

Description 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 (

Learning from the mistakes 258

[leetcode]#797. All Paths From Source to Target

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.  g...

m0_37943543的博客 331

leetcode 797. All Paths From Source to Target(python3)

leetcode 797. All Paths From Source to Target(python3) 原题地址:https://leetcode.com/problems/all-paths-from-source-to-target/ 题目 Given a directed, acyclic graph of N nodes. Find all possible pat...

birdreamer的博客 677

LeetCode 797. All Paths From Source to Target(所有可能的路径)

题目: 分析: 这题可以通过递归来解决,到了一个新的节点就通过graph数组查找它可以到达的下一个节点然后递归这些节点,在这个过程中不断把遍历到的节点添加到ArrayList中,找到目标节点后,把ArrayList添加到这个ArrayListAll中,ArrayListAll的需要在方法中传递,而ArrayList在递归调用前需要先copy一份,确保每个递归路径持有引用独立的ArrayList。...

马一凡的博客 387

LeetCode797. All Paths From Source to Target

【代码】LeetCode797. All Paths From Source to Target。

叶卡捷琳堡的博客 541

LeetCode-797. All Paths From Source to Target

Given a directed, acyclic graph ofNnodes. Find all possible paths from node0to nodeN-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1....

reigns的博客 361

leetcode 797. All Paths From Source to Target 所有可能的路径

邹九的个人博客 236

leetcode797. All Paths From Source to Target

797. All Paths From Source to Target

weixin_62377608的博客 93
上一篇: LeetCode 814. Binary Tree Pruning
下一篇: LeetCode 728. Self Dividing Numbers
ZayneHuang
博客等级 码龄10年 5粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值