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
/ \
\_/ Reference: https://www.cnblogs.com/springfor/p/3874591.html
/**
* 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;
}
//queue
Queue<UndirectedGraphNode> queue = new LinkedList<>();
//map
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>();
UndirectedGraphNode head = new UndirectedGraphNode(node.label); map.put(node, head);
queue.add(node);
while(!queue.isEmpty()){
UndirectedGraphNode curnode = queue.poll();
//check each neighbor
for(UndirectedGraphNode aneighbor: curnode.neighbors){
//if not visited,then add to queue
if(!map.containsKey(aneighbor)){
queue.add(aneighbor);
UndirectedGraphNode newneighbor = new UndirectedGraphNode(aneighbor.label);
map.put(aneighbor, newneighbor);
}
map.get(curnode).neighbors.add(map.get(aneighbor));
}
}
return head;
}
}

LeetCode-Microsoft-Clone Graph的更多相关文章

  1. [Leetcode Week3]Clone Graph

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

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

    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 ----- java

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

  4. 【leetcode】Clone Graph(python)

    类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...

  5. Java for LeetCode 133 Clone Graph

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

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

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

  7. Leetcode#133 Clone Graph

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

  8. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  9. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  10. 【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 ...

随机推荐

  1. Contiguous Array with Equal Number of 0 & 1

    2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...

  2. 超短reads(primer、barcode、UMI、index等)比对方法

    二代reads最短都有50bp,所以大家常用的比对工具都是不支持50bp以下的reads的比对的. 但是,在实际中,我们确实又有比对super short reads的需求. So,我找到了如下方法来 ...

  3. 请求和响应:类ActionController::Base ; 类ActionDispatch::Request

    扩展:ActionController::Base < Metal 2个基本主题: Get and Show do and redirect Requests 每个请求,由router决定了co ...

  4. 解决jenkins运行selenium测试出错的问题

    If you run Jenkins as a service in the background it won't open apps in the foreground. You may eith ...

  5. 3 python 基本数据类型

    1.python的基本数据类型 1.字符串 不可变数据类型 2.int //整除 %取余 bit_length() print(a.bit_length()) #打印某个数字类型的二进制长度 3.bo ...

  6. 浅浅的分析LED呼吸灯的实现和PWM的关系

    前言 在本周,我们在python课上做了一个实验,用ARDUINO使小LED灯模仿出呼吸灯的效果,实验进行的很成功,但是机器当仅输出高/低电平的时候是怎么样才能做到渐亮渐暗(输出电压)的变化呢?在这里 ...

  7. centos 7安装vmtools时提示The path "" is not a valid path to the xxx kernel headers.

    解决办法: yum -y install kernel-headers kernel-devel gcc reboot

  8. ssh: connect to host 192.168.11.180 port 22: Connection refused

    错误原因: 1.sshd 未安装:sudo apt-get install openssh-server 2.sshd 未启动:sudo net start sshd 3.防火墙:sudo ufw d ...

  9. quartz---的Scheduler

    quartz---的Scheduler 从这副图中可以很直观的看出来quartz的关系: 调度:Scheduler任务调度器,是实际执行任务调度的控制器.在spring中通过SchedulerFact ...

  10. spring cloud 学习(二)关于 Eureka 的学习笔记

    关于 Eureka 的学习笔记 个人博客地址 : https://zggdczfr.cn/ ,欢迎光临~ 前言 Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Sprin ...