当前位置: 首页 > wzjs >正文

景区网站建设外包公司一个人头挣多少钱

景区网站建设,外包公司一个人头挣多少钱,上海做网站的公司官网,车牌照损坏在网站做的能用吗给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序) graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向…

给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序

 graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。

示例 1:

输入:graph = [[1,2],[3],[3],[]]
输出:[[0,1,3],[0,2,3]]
解释:有两条路径 0 -> 1 -> 3 和 0 -> 2 -> 3

本题输入方式为邻接表方式输入

对于邻接表方式解题方法:

1、DFS遍历

2、BFS遍历

DFS遍历实现代码(基于邻接表)

class Solution {List<Integer> path=new ArrayList<>();List<List<Integer>> result=new ArrayList<>();public List<List<Integer>> allPathsSourceTarget(int[][] graph) {path.add(0);dfs(graph,0);return result;}private void dfs(int[][] graph,int x){if(x==graph.length-1){result.add(new ArrayList<>(path));return;}for(int i=0;i<graph[x].length;i++){path.add(graph[x][i]);dfs(graph,graph[x][i]);path.remove(path.size()-1);}}
}

BFS遍历实现代码(基于邻接表)

import java.util.*;class Solution {public List<List<Integer>> allPathsSourceTarget(int[][] graph) {List<List<Integer>> result = new ArrayList<>();int target = graph.length - 1;// BFS队列:存储(当前节点,路径)Queue<List<Integer>> queue = new LinkedList<>();// 初始路径:0List<Integer> initialPath = new ArrayList<>();initialPath.add(0);queue.offer(initialPath);while (!queue.isEmpty()) {List<Integer> currentPath = queue.poll();int lastNode = currentPath.get(currentPath.size() - 1);// 到达目标节点if (lastNode == target) {result.add(new ArrayList<>(currentPath));continue;}// 遍历邻接表中的所有邻居for (int neighbor : graph[lastNode]) {List<Integer> newPath = new ArrayList<>(currentPath); // 复制路径newPath.add(neighbor);queue.offer(newPath);}}return result;}
}

图论题目中除了邻接表还有邻接矩阵形式,可以将邻接表转换为邻接矩阵形式更为直观清晰。

邻接矩阵方式解题方法:

1、DFS遍历

2、BFS遍历

DFS遍历实现代码(基于邻接矩阵)

import java.util.*;class Solution {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();int[][] adjacencyMatrix; // 邻接矩阵public List<List<Integer>> allPathsSourceTarget(int[][] graph) {// 1. 将邻接表转换为邻接矩阵int n = graph.length;adjacencyMatrix = new int[n][n];for (int i = 0; i < n; i++) {for (int neighbor : graph[i]) {adjacencyMatrix[i][neighbor] = 1; // 标记存在边}}// 2. 从节点0开始DFSpath.add(0);dfs(0, n - 1);return result;}private void dfs(int current, int target) {if (current == target) {result.add(new ArrayList<>(path)); // 找到一条路径return;}for (int next = 0; next < adjacencyMatrix.length; next++) {if (adjacencyMatrix[current][next] == 1) { // 如果存在边path.add(next);dfs(next, target);path.remove(path.size() - 1); // 回溯}}}
}

BFS遍历实现代码(基于邻接矩阵)

import java.util.*;class Solution {public List<List<Integer>> allPathsSourceTarget(int[][] graph) {List<List<Integer>> result = new ArrayList<>();int n = graph.length;// 1. 将邻接表转换为邻接矩阵int[][] adjacencyMatrix = new int[n][n];for (int i = 0; i < n; i++) {for (int neighbor : graph[i]) {adjacencyMatrix[i][neighbor] = 1;}}// 2. BFS初始化:队列存储(当前节点,路径)Queue<Pair<Integer, List<Integer>>> queue = new LinkedList<>();List<Integer> initialPath = new ArrayList<>();initialPath.add(0);queue.offer(new Pair<>(0, initialPath));// 3. BFS遍历while (!queue.isEmpty()) {Pair<Integer, List<Integer>> current = queue.poll();int node = current.getKey();List<Integer> path = current.getValue();if (node == n - 1) {result.add(new ArrayList<>(path)); // 找到一条路径continue;}// 遍历所有邻接节点for (int next = 0; next < n; next++) {if (adjacencyMatrix[node][next] == 1) {List<Integer> newPath = new ArrayList<>(path); // 复制路径newPath.add(next);queue.offer(new Pair<>(next, newPath));}}}return result;}// 辅助Pair类(Java 14+可用java.util.Record替代)static class Pair<K, V> {K key;V value;Pair(K key, V value) {this.key = key;this.value = value;}K getKey() { return key; }V getValue() { return value; }}
}

 

 


文章转载自:

http://Sw3EVvwn.jwxmn.cn
http://vqcYoaSa.jwxmn.cn
http://KM93uxFl.jwxmn.cn
http://gNFrVPk2.jwxmn.cn
http://AZ5Q5AZ7.jwxmn.cn
http://sqzWqJu4.jwxmn.cn
http://FXdmGrsf.jwxmn.cn
http://Et6ulrB1.jwxmn.cn
http://rha7WvCB.jwxmn.cn
http://iuMLeZGX.jwxmn.cn
http://E7wcnlST.jwxmn.cn
http://HGP041oS.jwxmn.cn
http://Y2eDdb47.jwxmn.cn
http://wh76oEvN.jwxmn.cn
http://6r3R7v5q.jwxmn.cn
http://zs7Q9BZU.jwxmn.cn
http://Zun5465B.jwxmn.cn
http://zYuRlNu4.jwxmn.cn
http://Tdlnbus2.jwxmn.cn
http://bgKBkqTL.jwxmn.cn
http://B4DNIMwF.jwxmn.cn
http://5wPewOQm.jwxmn.cn
http://48rW8ZPO.jwxmn.cn
http://FVZ99g3w.jwxmn.cn
http://v7vjVRSf.jwxmn.cn
http://xpnv7GiY.jwxmn.cn
http://HC6O2Dmy.jwxmn.cn
http://Ah43bLVp.jwxmn.cn
http://WFj0lMot.jwxmn.cn
http://BmgzmvO6.jwxmn.cn
http://www.dtcms.com/wzjs/636601.html

相关文章:

  • wordpress卡密网站源码跨境电商最好的平台
  • 网站建设一般需要多少钱一个网站可以优化多少关键词
  • 江苏省建设厅网站是网页qq邮箱登录入口
  • 金融企业网站源码重庆排名前十的互联网公司
  • 淘宝客网站的模板学院网站建设需求分析
  • 新乡商城网站建设哪家专业制作做动画的网站
  • 长沙哪个网站建设最好珠宝网站模版
  • 网站推广seo方法网站建设开发教程视频
  • 网站软文代写网站建设费摊销
  • 建立一个网站的步骤汕头百姓网二手房出售
  • 网站建设与管理实验目的道可道在线设计平台
  • 汽车用品网站网站建设引领者
  • 全球访问量最大的10个网站地产网站互动营销
  • 亿客搜网站建设英文商务网站制作
  • 网站页面设计论文怎么免费自己做网站
  • 慈溪网站制作哪家最便宜公司介绍ppt范例内容
  • 合肥的网站建设网站建设 织梦者
  • 自己做菠菜网站有什么网上做c 的网站
  • 设计头像网站免费推荐商城网站建设需要
  • 网站工程和网络工程有什么区别网站微信支付开发
  • 邢台移动网站建设公司做网站需要哪些素材
  • 手机一元云购网站建设python基础教程第二版课后答案
  • 网页设制作与网站建设宝典 pdf品牌设计ppt案例
  • 肇庆企业建站模板南京网站如何制作
  • 综合门户类网站有哪些网站设计与建设工作室
  • 诸塈市建设局网站掌上大学微信管理系统
  • 网站备案填写要求吗分形科技做网站怎么样
  • 教育信息化建设网站wordpress解密主题
  • 汕头如何建设网站设计微信号注册官网网页版
  • 网站建设外包注意事项网站的结构包括哪些内容