【leetcode】Clone Graph
来源:互联网 发布:桌面办公软件 编辑:程序博客网 时间:2024/05/17 04:37
题目:
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 / \ \_/
思路:
首先做一次BFS遍历,将所有结点创建出来。并且要保存在一个可以在短时间读取的结构中。map是个不错的结构并且每个结点label唯一,因此我们可以用label来作为索引。
第二次遍历的时候可以快速地找出结点并构造图的关系。
代码:
- /**
- * Definition for undirected graph.
- * class UndirectedGraphNode {
- * int label;
- * ArrayList<UndirectedGraphNode> neighbors;
- * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
- * };
- */
- public class Solution {
- Map<Integer, UndirectedGraphNode> nodeMap = new HashMap<Integer, UndirectedGraphNode>();
- public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
- // Note: The Solution object is instantiated only once and is reused by each test case.
- if (node == null) {
- return null;
- }
- LinkedList<UndirectedGraphNode> stack = new LinkedList<UndirectedGraphNode>();
- //Map<Integer, UndirectedGraphNode> nodeMap = new HashMap<Integer, UndirectedGraphNode>();
- nodeMap.clear();
- Set<Integer> isFound = new HashSet<Integer>();
- stack.addLast(node);
- UndirectedGraphNode head = null;
- while (!stack.isEmpty()) {
- UndirectedGraphNode first = stack.getFirst();
- stack.removeFirst();
- int label = first.label;
- UndirectedGraphNode n = nodeMap.get(label);
- if (n == null) {
- n = new UndirectedGraphNode(label);
- nodeMap.put(label, n);
- }
- if (head == null) {
- head = n;
- }
- if (!isFound.contains(label)) {
- isFound.add(label);
- ArrayList<UndirectedGraphNode> neighbors = first.neighbors;
- for (UndirectedGraphNode nb : neighbors) {
- UndirectedGraphNode nn = nodeMap.get(nb.label);
- if (nn == null) {
- nn = new UndirectedGraphNode(nb.label);
- nodeMap.put(nb.label, nn);
- stack.addLast(nb);
- }
- n.neighbors.add(nn); }
- }
- }
- return head;
- }
- }
0 0
- LeetCode[Graph]: Clone Graph
- [LeetCode] CLone Graph
- LeetCode:Clone Graph
- [leetcode]Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- LeetCode题解:Clone Graph
- [LeetCode]Clone Graph
- leetcode Clone Graph
- leetcode-Clone Graph
- LeetCode | Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- LeetCode OJ:Clone Graph
- Leetcode Clone Graph
- [Leetcode] Clone Graph (Java)
- Clone Graph - LeetCode
- 【leetcode】Clone Graph
- 【那些年学过的计算机基础】--计算机基本部件和接口
- 黑马程序员_IO3_其他的IO流
- 在opensuse 13.1使用plymouth更换开关机画面
- Cocos2d-x学习之windows 7 android环境搭建
- linux中查找命令find、locate、whereis、which、type区别
- 【leetcode】Clone Graph
- hdu 3938 Portal(离线并查集)
- win8下引导win7、XP及开启F8、开启图形界面启动的方法
- Yii: 批量插入数据的扩展类简单实现
- HDU2092
- 恢复F8(恢复到出厂设置)功能
- HDOJ 3401 Trade
- 第一笑话王
- EasyX Library for C++ (Ver:2013冬至版)头文件整理