题目

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

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (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相关推荐

  1. 133. Clone Graph

    欢迎fork and star:Nowcoder-Repository-github 133. Clone Graph 题目 Clone an undirected graph. Each node ...

  2. 【重点!DFS/记忆化递归 + BFS】LeetCode 133. Clone Graph

    LeetCode 133. Clone Graph Solution1: DFS/记忆化递归,参考网址:http://www.cnblogs.com/grandyang/p/4267628.html ...

  3. [leetcod] Clone Graph

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  4. leetcode -- Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. [解题思 ...

  5. Clone Graph问题及解法

    问题描述: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors ...

  6. LeetCode Clone Graph(搜索问题)

    问题:给出一个邻接列表表示的图,要求克隆这个图 思路: 1.深度优先搜索,使用map记录结点与其克隆结点之间的映射关系 .当遍历结点时,如果在map中存在结点的克隆,则直接返回.否则创建结点的克隆,加 ...

  7. 133. Clone Graph 克隆图

    给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node {public in ...

  8. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 1 /* 2 // Definition for a Node. 3 ...

  9. leetcode解题文件夹

    点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...

最新文章

  1. php 复选框 单选 全选,复选框全选/不全选,选择结果提交
  2. Go学习笔记07-结构体与方法
  3. Spring MVC-05循序渐进之数据绑定和form标签库(下) 实战从0到1
  4. hdfs中与file数组类似的数组_Chapter05 Java中的数组
  5. 讲一讲应用服务的新鲜事儿
  6. 牛客网【每日一题】Shortest Path 4月3日题目精讲 DFS
  7. linux学习第四周作业练习
  8. 马斯克召集百名员工测试完全自动驾驶,1.3万美元大优惠!先到先得
  9. Spring Boot学习——统一异常处理
  10. [转帖]到底什么是时间复杂度
  11. IOS Xcode7 http 和 https
  12. 计算机青蓝云题库,计算机三级上机题库 计算机三级网络技术上机题库《南开100题》.doc...
  13. windows11虚拟机安装失败解决办法
  14. Tomcat 漏洞修复建议
  15. PHP 蚂蚁芝麻信用分接口
  16. UVALive 6657 GCD XOR
  17. RTL8188 Linux驱动移植
  18. 畅想小组KTV点歌系统简介
  19. Linux命令简写与全称
  20. 126篇殿堂级深度学习论文分类整理 从入门到应用

热门文章

  1. CCF CSP 201612-1中位数(满分代码)
  2. 日常笔记-css\html篇
  3. Python机器学习--回归
  4. GitLab公布关于开发者趋势的问卷调查结果
  5. 冒烟测试与回归测试的区别
  6. 如何利用SEO做好网站推广
  7. 读《编程之道》-对于程序员的抽象描述
  8. 技术社区,你真的会混吗?
  9. [转载]关于request和session详解
  10. Spring配置文件详解三:Spring声明式事务管理