[LeetCode] Clone Graph 无向图的复制
2015-02-02 13:56
274 查看
Clone an undirected graph. Each node in the graph contains a
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use
As an example, consider the serialized graph
The graph has a total of three nodes, and therefore contains three parts as separated by
First node is labeled as
Second node is labeled as
Third node is labeled as
Visually, the graph looks like the following:
这道无向图的复制问题和之前的拷贝带有随机指针的链表有些类似,那道题的难点是如何处理每个节点的随机指针,这道题目的难点在于如何处理每个节点的neighbors,由于在深度拷贝每一个节点后,还要将其所有neighbors放到一个vector中,而如何避免重复拷贝呢?这道题好就好在所有节点值不同,所以我们可以使用哈希表来对应节点值和新生成的节点。对于图的遍历的两大基本方法是深度优先搜索DFS和广度优先搜索BFS,此题的两种解法可参见网友爱做饭的小莹子的博客,这里我们使用深度优先搜索DFS来解答此题,代码如下:
LeetCode All in One 题目讲解汇总(持续更新中...)
labeland 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
0to both nodes
1and
2.
Second node is labeled as
1. Connect node
1to node
2.
Third node is labeled as
2. Connect node
2to node
2(itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
这道无向图的复制问题和之前的拷贝带有随机指针的链表有些类似,那道题的难点是如何处理每个节点的随机指针,这道题目的难点在于如何处理每个节点的neighbors,由于在深度拷贝每一个节点后,还要将其所有neighbors放到一个vector中,而如何避免重复拷贝呢?这道题好就好在所有节点值不同,所以我们可以使用哈希表来对应节点值和新生成的节点。对于图的遍历的两大基本方法是深度优先搜索DFS和广度优先搜索BFS,此题的两种解法可参见网友爱做饭的小莹子的博客,这里我们使用深度优先搜索DFS来解答此题,代码如下:
/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */ class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { unordered_map<int, UndirectedGraphNode*> umap; return clone(node, umap); } UndirectedGraphNode *clone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &umap) { if (!node) return node; if (umap.count(node->label)) return umap[node->label]; UndirectedGraphNode *newNode = new UndirectedGraphNode(node->label); umap[node->label] = newNode; for (int i = 0; i < node->neighbors.size(); ++i) { (newNode->neighbors).push_back(clone(node->neighbors[i], umap)); } return newNode; } };
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- leetcode第18题(clone-graph)
- Clone Graph [Leetcode解题报告]
- Leetcode: Clone Graph
- 【LeetCode】Clone Graph 解题报告
- LeetCode: Clone Graph
- LeetCode 133 Clone Graph
- [Leetcode]Clone Graph
- Leetcode: Clone Graph
- leetcode Clone Graph
- [LeetCode] Clone Graph(!!!!graph&dfs&bfs)
- [LeetCode]Clone Graph
- leetcode——133——Clone Graph
- LeetCode(133)Clone a Graph
- 【leetcode】clone-graph
- LeetCode 133 Clone Graph (BFS || DFS)
- Clone Graph [leetcode] dfs和bfs
- leetcode — clone-graph
- [Leetcode 133, Medium] Clone Graph