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 node 0 to both nodes 1 and 2.
    Second node is labeled as 1. Connect node 1 to node 2.
    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
         / \
         \_/
解题思路:

BFS或DFS均可做出,BFS的JAVA实现如下:

  1. public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
  2. if (node == null)
  3. return null;
  4. Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
  5. HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
  6. UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
  7. queue.add(node);
  8. map.put(node, newHead);
  9.  
  10. while (!queue.isEmpty()) {
  11. UndirectedGraphNode curr = queue.poll();
  12. List<UndirectedGraphNode> currNeighbors = curr.neighbors;
  13. for (UndirectedGraphNode aNeighbor : currNeighbors)
  14. if (!map.containsKey(aNeighbor)) {
  15. UndirectedGraphNode copy = new UndirectedGraphNode(
  16. aNeighbor.label);
  17. map.put(aNeighbor, copy);
  18. map.get(curr).neighbors.add(copy);
  19. queue.add(aNeighbor);
  20. } else
  21. map.get(curr).neighbors.add(map.get(aNeighbor));
  22. }
  23. return newHead;
  24. }

DFS 实现如下:

  1. Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
  2.  
  3. public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
  4. if (node == null)
  5. return null;
  6. if (map.containsKey(node))
  7. return map.get(node);
  8. UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
  9. map.put(node, newHead);
  10. for (UndirectedGraphNode aNeighbor : node.neighbors)
  11. newHead.neighbors.add(cloneGraph(aNeighbor));
  12. return newHead;
  13. }

Java for LeetCode 133 Clone Graph的更多相关文章

  1. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  2. leetcode 133. Clone Graph ----- java

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  3. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  4. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

  5. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  6. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  7. 133. Clone Graph

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

  8. [Leetcode Week3]Clone Graph

    Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...

  9. 133. Clone Graph (3 solutions)——无向无环图复制

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

随机推荐

  1. Linux shell 提取文件名和目录名的一些方法(转)

    很多时候在使用Linux的shell时,我们都需要对文件名或目录名进行处理,通常的操作是由路径中提取出文件名,从路径中提取出目录名,提取文件后缀名等等.例如,从路径/dir1/dir2/file.tx ...

  2. k8s学习(一)——kubectl与api-server之间的交互核心过程

    k8s的架构是用户使用kubectl工具对虚拟机资源进行各种各样的控制和定制. 而kubectl本身并不包含对其核心资源的访问与控制.而是通过http通信与api-server进行交互实现资源的管理. ...

  3. Linux系统救援模式应用:单用户模式找回密码

    利用Linux系统救援模式找回密码 方法一: 开机时手要快按任意键,因为默认时间5s grub菜单,只有一个内核,没什么好上下选的,按e键.升级了系统或安装了Xen虚拟化后,就会有多个显示. 接下来显 ...

  4. JAVA Eclipse创建Android程序如何实现MainActivity和Fragment相互传递数据

    最简单的方法是直接强制生成一个MainActivity的实例,然后可以执行其中的方法,当然也是可以传递参数的 更加复杂的用法可以参考: http://blog.csdn.net/huangyabin0 ...

  5. Solidworks如何使用Toolbox

    Toolbox不仅仅是智能扣件.事实上,一般常见的轴承,螺栓,齿轮都有了,点击右侧的设计库即可展开Toolbox   配置完成后我只留下一个GB   比如我要选一个圆锥滚子轴承,从右边拖进来即可   ...

  6. C 作用域规则

    C 作用域规则 任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问.C 语言中有三个地方可以声明变量: 在函数或块内部的局部变量 在所有函数外部的全局变量 在形式参数的函 ...

  7. 浅谈PHP与手机APP开发即API接口开发

    API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞生很多专门API服务的公司,如:聚合数据(http ...

  8. 文件I/O操作为什么叫输入/出流

    参考以下文档: http://blog.csdn.net/hguisu/article/details/7418161 我们关注的焦点是错误的,重点不在文件,我们关注的核心是数据流. 这种流可以是文本 ...

  9. iOS_GET_网络请求

    同步的 get 请求 #pragma mark - 同步的 get 请求 - (IBAction)GETSynButtonDidClicked:(UIButton *)sender { // 1.网址 ...

  10. idea设置自定义图片

    看图操作哈: 1. 2. 逼格满满: