133. Clone Graph
来源:互联网 发布:网络玄幻小说典藏版 编辑:程序博客网 时间:2024/05/29 13:35
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 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 / \ \_/
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */public class Solution { private HashMap<Integer, UndirectedGraphNode> map = new HashMap<Integer, UndirectedGraphNode>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return null; } if (map.containsKey(node.label)) { return map.get(node.label); } UndirectedGraphNode clone = new UndirectedGraphNode(node.label); map.put(clone.label, clone); for (UndirectedGraphNode neighborNode: node.neighbors) { clone.neighbors.add(cloneGraph(neighborNode)); } return clone; }}
阅读全文
0 0
- LeetCode 133. Clone Graph
- [Leetcode] 133. Clone Graph
- [leetcode] 133.Clone Graph
- 133. Clone Graph
- Leetcode 133. Clone Graph
- 133. Clone Graph LeetCode
- 133. Clone Graph
- LeetCode 133. Clone Graph
- leetcode 133. Clone Graph
- 133. Clone Graph
- 133. Clone Graph
- LeetCode *** 133. Clone Graph
- leetcode-133. Clone Graph
- LeetCode 133. Clone Graph
- 133. Clone Graph
- 133. Clone Graph
- 133. Clone Graph
- 133. Clone Graph
- 简单的在线留言系统
- leetcode-java.T012_IntegerToRoman 数字转罗马数字
- HackerRank
- 箭头函数写法在ReactJs中的使用
- 面试题目
- 133. Clone Graph
- RAC 中的双向数据绑定 RACChannel
- leetcode-java.T011_ContainerWithMostWater 找两条竖线然后这两条线以及X轴构成的容器能容纳最多的水
- Android快速实现热更新
- 数学符号
- leetcode-java.T009_PalindromeNumber 回文数字
- Python 父子类继承笔记
- leetcode-java.T008_StringToInteger 字符串转成整形
- 解决安卓style文件中设置Activity退出动画无效或者被干扰的问题