[LeetCode]: 133: Clone Graph
题目:
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use #
as a separator for each node, and ,
as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}
.
The graph has a total of three nodes, and therefore contains three parts as separated by #
.
- First node is labeled as
0
. Connect node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1/ \/ \0 --- 2/ \\_/
题目分析:
题目本身就是个图的遍历问题,其实只用看最上面的一句就OK了。下面整这么一篇就是为了说明Testcase,结果误导对了我好久。
与树的遍历不同之处就是图可能会出现"闭合的环"这样会造成无线循环,所以遍历的时候需要把已经遍历过的点去掉。
根据题目,图中每一个点的权值是不一样的,所以可以采用取巧的方法检测这个点是否已经加在了图中,即使用HashMap
思路一:深度优先搜索
public static UndirectedGraphNode cloneDFS(UndirectedGraphNode node,HashMap<Integer, UndirectedGraphNode> validator){if(validator.containsKey(node.label)){return validator.get(node.label);}UndirectedGraphNode nodeTemp = new UndirectedGraphNode(node.label);validator.put(node.label, nodeTemp);for(int i =0;i<node.neighbors.size();i++){nodeTemp.neighbors.add(cloneDFS(node.neighbors.get(i),validator));}return nodeTemp;}public static UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {if(node== null || node.equals(null)){return null;}HashMap<Integer, UndirectedGraphNode> validator = new HashMap<Integer, UndirectedGraphNode>();return cloneDFS(node, validator);}
思路二:广度优先搜索
XXX
转载于:https://www.cnblogs.com/savageclc26/p/4884900.html
[LeetCode]: 133: Clone Graph相关推荐
- 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph
LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...
- 133. Clone Graph
欢迎fork and star:Nowcoder-Repository-github 133. Clone Graph 题目 Clone an undirected graph. Each node ...
- 133. Clone Graph 克隆图
给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node {public in ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...
- leetcode -- Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. [解题思 ...
- LeetCode Clone Graph(搜索问题)
问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...
- Clone Graph
题目 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. O ...
- leetcode 785. Is Graph Bipartite? | 785. 判断二分图(DFS,地图着色)
题目 https://leetcode.com/problems/is-graph-bipartite/ 题解 有点像简化版的地图着色问题. 每走一步,颜色翻转一下,并且染色. 当遇到已经染过色的节点 ...
- [leetcod] Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Leetcode 133.克隆图
Time: 20190903 Type: Medium 题目描述 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆).图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[No ...
最新文章
- 独家 | 6大角度击破数据科学面试套路!助你找到理想工作
- 8GB内存的树莓派4来啦!国内售价589元,官方还推出配套64位系统
- Linux执行yum不显示时间,Linux停的yum命令详解(朝花夕拾)
- javascript系列-class10.DOM(下)
- tomcat启动报错The JRE could not be found.Edit the server and change the JRE location
- Atcoder 084D - Small Multiple(最短路径+思维)
- ospf工作原理_OSPF动态路由配置经典案例
- 如何评价周志华深度森林模型
- 操作系统:图文详解 内存映射
- 人工智能是一个工程问题,而不是魔术!
- ES11新特性_动态import---JavaScript_ECMAScript_ES6-ES11新特性工作笔记065
- php对接监控摄像头源码,摄像头监控录像源代码 (详细的代码,可以直接使用,也可以拿来学习使用)...
- MyEclipse设置字体大小
- 价值工程杂志价值工程杂志社价值工程编辑部2022年第23期目录
- ItextPdf实现页码
- 设计模式之禅(一)——六大设计原则
- 《网络攻防技术与实践》(诸葛建伟)读书笔记
- 百度云下载神器再升级!
- Java 习题 (12)
- 【软工Work1】四则加减乘除混合运算(带括号、真分数)