LeetCode-Microsoft-Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
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 node0to both nodes1and2. - Second node is labeled as
1. Connect node1to node2. - Third node is labeled as
2. Connect node2to node2(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的更多相关文章
- [Leetcode Week3]Clone Graph
Clone Graph题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/clone-graph/description/ Description Clon ...
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 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 ...
- 【leetcode】Clone Graph(python)
类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...
- 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 ...
- [leetcode]133. Clone Graph 克隆图
题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node neighbor 1 2, 3 2 1 3 1 ...
- Leetcode#133 Clone Graph
原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- 【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 ...
随机推荐
- 关于c#除法运算的问题
https://blog.csdn.net/yxt1522916229/article/details/51107569/ 下面的示例可以验证一下问题: 例如: int m = 2; ...
- 3.6 MIPS指令简介
计算机组成 3 指令系统体系结构 3.6 MIPS指令简介 MIPS秉承着指令数量少,指令功能简单的设计理念.那这样的设计理念是如何实现的呢?在这一节,我们就将来分析MIPS指令的特点. 相比于X86 ...
- 『Python』pycharm常用设置
学习一下pycharm的快捷操作,提升速度,也提升舒适度,笑. 常用快捷键 ctrl + d :复制粘贴本行到下一行 ctrl + y :删除本行 ctrl + 鼠标点击 :跳转 ctrl + / : ...
- hdu2059 dpdpdp玄学5555~~
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- android升级adt和sdk之后无法识别SDK Location的一个解决方式
我把android的adt和sdk从4.0升级到4.2,发现eclipse的android设置里面原来列出的各种api level的platform消失了,而且无法新建android工程.而且检查过了 ...
- 新手如何正确使用CLion之输出hello world
以前只使用过vc6.0,在用过jetbrain的pycharm后就考虑换个c++的编译器了,第一还是考虑了vs2017但用惯了色彩鲜艳的jb产品后竟然有点不习惯vs,最后还是果断选择了jb的CLion ...
- SGU 110. Dungeon 计算几何 难度:3
110. Dungeon time limit per test: 0.25 sec. memory limit per test: 4096 KB The mission of space expl ...
- 关于双网卡双宽带Http及Socks代理的配置
1.[硬件环境] a, 1台宿主(win7)+几十台虚拟机(xp)(vm10的版本,估计可打开52台以上的虚拟机) b, 双网卡,其中一个网卡通过路由连接电信ADSL,一个直连集线器,可直接连接移动m ...
- EasyGui 学习文档【超详细中文版】
演示使用 Python 3.3.3 版本 0. 安装 EasyGui 官网:http://easygui.sourceforge.net 最新版: <ignore_js_op> easyg ...
- NodeJS express不是外部或内部命令的解决办法
今天在用express生成项目的时候,使用cmd命令窗口发现express不是“内部或外部命令......”.但是我已经使用了npm install -g express成功安装了express模块插 ...