leetcode -- Clone Graph
Clone an undirected graph. Each node in the graph contains a label
and a list of its neighbors
.
[解题思路]
图的遍历有两种方式,BFS和DFS
这里使用BFS来解本题,BFS需要使用queue来保存neighbors
但这里有个问题,在clone一个节点时我们需要clone它的neighbors,而邻居节点有的已经存在,有的未存在,如何进行区分?
这里我们使用Map来进行区分,Map的key值为原来的node,value为新clone的node,当发现一个node未在map中时说明这个node还未被clone,
将它clone后放入queue中处理neighbors。
使用Map的主要意义在于充当BFS中Visited数组,它也可以去环问题,例如A--B有条边,当处理完A的邻居node,然后处理B节点邻居node时发现A已经处理过了
处理就结束,不会出现死循环!
queue中放置的节点都是未处理neighbors的节点!!!!
1 /** 2 * Definition for undirected graph. 3 * class UndirectedGraphNode { 4 * int label; 5 * ArrayList<UndirectedGraphNode> neighbors; 6 * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 7 * }; 8 */ 9 public class Solution { 10 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 11 // Note: The Solution object is instantiated only once and is reused by each test case. 12 if(node == null){ 13 return node; 14 } 15 UndirectedGraphNode result = new UndirectedGraphNode(node.label); 16 LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); 17 queue.add(node); 18 Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); 19 map.put(node, result); 20 21 while(!queue.isEmpty()){ 22 UndirectedGraphNode nodeInQueue = queue.poll(); 23 ArrayList<UndirectedGraphNode> neighbors = nodeInQueue.neighbors; 24 for(int i = 0; i < neighbors.size(); i++){ 25 UndirectedGraphNode n1 = neighbors.get(i); 26 if(map.containsKey(n1)){ 27 map.get(nodeInQueue).neighbors.add(map.get(n1)); 28 } else { 29 UndirectedGraphNode n1clone = new UndirectedGraphNode(n1.label); 30 map.get(nodeInQueue).neighbors.add(n1clone); 31 map.put(n1, n1clone); 32 queue.add(n1); 33 } 34 } 35 36 } 37 return result; 38 } 39 }
ref:
http://leetcode.com/2012/05/clone-graph-part-i.html
leetcode -- Clone Graph相关推荐
- LeetCode Clone Graph(搜索问题)
问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...
- 【重点!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 ...
- Clone Graph
题目 Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. O ...
- [leetcod] Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Clone Graph问题及解法
问题描述: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors ...
- 133. Clone Graph 克隆图
给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node {public in ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- 【读书笔记】剑指offer
导语 所有的编程练习都在牛客网OJ提交,链接: https://www.nowcoder.com/ta/coding-interviews 九章算法的 lintcode 也有这本书的题目.https: ...
最新文章
- 阿里云发布三大人工智能产品:推动AI产业落地
- Verdi GUI技能操作-Application Tutorials部分
- [ARM-assembly]-ARM向量浮点指令集 快速参考卡
- 7 Javascript:表单与验证-非空验证
- iis php重定向循环,iis和apache及PHP页面设置301重定向跳转方法
- EndNote使用技巧之一--参考文献的导入
- 获得连接对象 jdbc 20210412_204123.mp4
- SharePoint 2013 中的新增功能(与开发有关)
- 手把手教你玩转YOLOX--Windows(上)
- python itchat文档_python itchat简介
- Java敏感词过滤 工具类
- 微信小程序tabBar边框加|上阴影
- 聚类有效性指标(CVI)
- 饥饿游戏3:嘲笑鸟(下)[The Hunger Games: Mockingjay - Part 2]
- Android手机下载的缓存视频如何找到?
- Python Tkinter——数字拼图游戏
- 获取微信公众号文章封面图的技巧/网站
- 苹果付费app共享公众号_【苹果付费APP共享】 Affinity Photo
- java treemap字母排序_Java TreeMap对数字,英文字母,汉字等排序例子
- 事业单位计算机操作专业技术分析面试题,事业单位面试:历年面试试题解析(四)...