[leetcode] Clone Graph
来源:互联网 发布:淘宝扣分处罚节点 编辑:程序博客网 时间:2024/06/14 10:34
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. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution {public: map<int,UndirectedGraphNode*> lab; UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(node==NULL) return NULL; UndirectedGraphNode *res; res=new UndirectedGraphNode(node->label); lab[node->label]=res; for(int i=0;i<node->neighbors.size();i++){ if(lab.find(node->neighbors[i]->label)==lab.end()) res->neighbors.push_back(cloneGraph(node->neighbors[i])); else res->neighbors.push_back(lab[node->neighbors[i]->label]); } return res; }};
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
- JAVA中String与StringBuffer 、StringBuilder的区别
- HIT OJ 2060 Fibonacci Problem Again(矩阵快速幂)
- popupwindow弹出,父类背景变暗问题
- android 调用系统的接口
- 计算机网路体系结构
- [leetcode] Clone Graph
- “EXC_BAD_INSTRUCTION"异常的问题!
- javascript判断checkbox是否选中
- 老罗的android视频教程
- 判断字符串最后一个字符是否为.号
- python数字即数学函数
- mongodb中的更新和插入操作
- 线段树 基础
- Cracking coding iterview(1.6)Rotate NXN Matrix