Leetcode 133. Clone Graph (Medium) (cpp)
来源:互联网 发布:虚拟男朋友聊天软件 编辑:程序博客网 时间:2024/05/17 15:01
Leetcode 133. Clone Graph (Medium) (cpp)
Tag: Depth-first Search, Breadth-first Search, Graph
Difficulty: Medium
/*133. Clone Graph (Medium)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/ \\_/*//*** 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 == NULL) {return NULL;}unordered_map<int, UndirectedGraphNode*> mapping;UndirectedGraphNode *newhead = new UndirectedGraphNode(node->label);mapping[node->label] = newhead;queue<UndirectedGraphNode*> q;q.push(node);while (!q.empty()) {UndirectedGraphNode *node1 = q.front();q.pop();for (UndirectedGraphNode* nei : node1->neighbors) {if (mapping.find(nei->label) == mapping.end()) {mapping[nei->label] = new UndirectedGraphNode(nei->label);q.push(nei);}mapping[node1->label]->neighbors.push_back(mapping[nei->label]);}}return newhead;}};
0 0
- Leetcode 133. Clone Graph (Medium) (cpp)
- Leetcode: 133. Clone Graph(Week6, Medium)
- [Leetcode 133, Medium] Clone Graph
- leetcode 日经贴,Cpp code -Clone Graph
- LeetCode 133. Clone Graph
- [Leetcode] 133. Clone Graph
- [leetcode] 133.Clone Graph
- Leetcode 133. Clone Graph
- 133. Clone Graph LeetCode
- LeetCode 133. Clone Graph
- leetcode 133. Clone Graph
- LeetCode *** 133. Clone Graph
- leetcode-133. Clone Graph
- LeetCode 133. Clone Graph
- [LeetCode] 133. Clone Graph
- LeetCode: 133. Clone Graph
- Leetcode-133. Clone Graph
- Leetcode 133. Clone Graph
- UMR与黑白熊
- HiveServer2 源码分析
- OkHttp的使用分析,okhttp的多种getpost方式
- gitlab 上源码Merge后出现git gc的解决方案
- CodeForces745C C - Hongcow Builds A Nation 图论+容斥
- Leetcode 133. Clone Graph (Medium) (cpp)
- 证明碰撞集问题为NP完全问题。
- 2016.12.24总结
- 直播123
- Hive CLI执行流程分析
- 约瑟夫问题 循环单链表解法
- 卷积神经网络(CNN)及其theano实现
- nginx负载均衡后tomcat获取客户端真实IP
- TensorFlow学习(三):Graph和Session