Clone Graph
题目
Clone an undirected graph. Each node in the graph contains a
label
and a list of itsneighbors
.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/ \\_/
方法
主要分两步:第一步创建全部的结点。第二步,创建结点的neighbors/*** Definition for undirected graph.* class UndirectedGraphNode {* int label;* List<UndirectedGraphNode> neighbors;* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }* };*/ public class Solution {public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {if (node == null) {return null;}Map<Integer, UndirectedGraphNode> map = new HashMap<Integer, UndirectedGraphNode>();Map<Integer, UndirectedGraphNode> graph = new HashMap<Integer, UndirectedGraphNode>();Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();queue.offer(node);while(!queue.isEmpty()) {UndirectedGraphNode temp = queue.poll();if (!map.containsKey(temp.label)) {UndirectedGraphNode newTemp = new UndirectedGraphNode(temp.label);graph.put(temp.label, temp);map.put(temp.label, newTemp);}for (UndirectedGraphNode neighbor : temp.neighbors) {if (!map.containsKey(neighbor.label)) {queue.offer(neighbor);}}}for (int label : graph.keySet()) {UndirectedGraphNode temp = graph.get(label);UndirectedGraphNode cloneTemp = map.get(temp.label);for (UndirectedGraphNode neighbor : temp.neighbors) {cloneTemp.neighbors.add(map.get(neighbor.label));}}return map.get(node.label);} }
Clone Graph相关推荐
- 133. Clone Graph
欢迎fork and star:Nowcoder-Repository-github 133. Clone Graph 题目 Clone an undirected graph. Each node ...
- 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph
LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...
- [leetcod] Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- leetcode -- 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 ...
- LeetCode Clone Graph(搜索问题)
问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...
- 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解题文件夹
点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...
最新文章
- php 复选框 单选 全选,复选框全选/不全选,选择结果提交
- Go学习笔记07-结构体与方法
- Spring MVC-05循序渐进之数据绑定和form标签库(下) 实战从0到1
- hdfs中与file数组类似的数组_Chapter05 Java中的数组
- 讲一讲应用服务的新鲜事儿
- 牛客网【每日一题】Shortest Path 4月3日题目精讲 DFS
- linux学习第四周作业练习
- 马斯克召集百名员工测试完全自动驾驶,1.3万美元大优惠!先到先得
- Spring Boot学习——统一异常处理
- [转帖]到底什么是时间复杂度
- IOS Xcode7 http 和 https
- 计算机青蓝云题库,计算机三级上机题库 计算机三级网络技术上机题库《南开100题》.doc...
- windows11虚拟机安装失败解决办法
- Tomcat 漏洞修复建议
- PHP 蚂蚁芝麻信用分接口
- UVALive 6657 GCD XOR
- RTL8188 Linux驱动移植
- 畅想小组KTV点歌系统简介
- Linux命令简写与全称
- 126篇殿堂级深度学习论文分类整理 从入门到应用