133. Clone Graph
来源:互联网 发布:小米网络解锁 编辑:程序博客网 时间:2024/05/18 03:25
/*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 node 0 to both nodes 1 and 2.Second node is labeled as 1. Connect node 1 to node 2.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 / \ \_/克隆图的一个难点就是一个结点的邻居可能在已经出现过,这样你只要把他的指针加到邻居集合中即可,也有可能这个结点还没出现过,因此你需要新建一个这个结点,因此我们需要一个hash表来对结点做一一映射每次搜索的时候看这个结点是不是已经被创建,是的话就返回其copy,否则就创建,然后再依次深度遍历其邻居结点并将其加入邻居集合中去.*//** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if(!node) return NULL; unordered_map<UndirectedGraphNode*,UndirectedGraphNode*> mp; return clone(node,mp); } UndirectedGraphNode *clone(UndirectedGraphNode *node,unordered_map<UndirectedGraphNode*,UndirectedGraphNode*>&mp){ if(!node) return NULL; if(mp.find(node)!=mp.end())//查看节点是否已经生成 return mp[node]; mp[node]=new UndirectedGraphNode(node->label); for(auto n : node->neighbors) mp[node]->neighbors.push_back(clone(n,mp)); return mp[node]; }};
阅读全文
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
- Python 画立方体
- 微信小程序开发整理
- Android——OnFocuChangeListener焦点事件
- 机架感知(Rack Awareness)机制浅述
- Mysql数据库在unity中的打开方式
- 133. Clone Graph
- 通过this调用重载构造函数时报错
- Python 画五角星
- JS的学习方法和技巧
- 三栏式布局
- 学习笔记(四)集合、文件与函数
- jenkins --忘记密码修改密码
- 【原创】kafka安装与启动
- TomCat工作原理