LeetCode133:Clone Graph
2014-02-19 08:51
441 查看
题目:
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:
解题思路:
实现代码:
Clone an undirected graph. Each node in the graph contains a
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 / \ \_/
解题思路:
本题要解决的问题就是对一个图进行clone,这不禁让我们想起图的遍历:DFS和BFS,所以我们可以再遍历图中某个点时,增加一些附加的操作而不仅仅是访问它,这里我们要增加的附加操作即对该点进行clone。
现在我们利用DFS和BFS两种方式进行解题。
实现代码:
#include <iostream> #include <vector> #include <queue> #include <unordered_map> using namespace std; /* 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 / \ \_/ */ 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*> hashtable;//标志位,是否已拷贝了key为label,value为新拷贝的node return dfsclone(node, hashtable); } //DFS UndirectedGraphNode *dfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable) { if(node == NULL) return NULL; if(hashtable.count(node->label) > 0) return hashtable[node->label];//已经拷贝好了,则直接返回即可,否则进行下面的拷贝操作 UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label); hashtable[copyNode->label] = copyNode; vector<UndirectedGraphNode *>::iterator iter; for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter) { copyNode->neighbors.push_back(dfsclone(*iter, hashtable));//进行深度优先算法拷贝 } return copyNode; } //BFS UndirectedGraphNode *bfsclone(UndirectedGraphNode *node, unordered_map<int, UndirectedGraphNode*> &hashtable) { if(node == NULL) return NULL; queue<UndirectedGraphNode *> qu; UndirectedGraphNode *copyNode = new UndirectedGraphNode(node->label); hashtable[copyNode->label] = copyNode; qu.push(node); while(!qu.empty()) { UndirectedGraphNode *tnode = qu.front(); qu.pop(); vector<UndirectedGraphNode *>::iterator iter; for(iter = node->neighbors.begin(); iter != node->neighbors.end(); ++iter) { if(hashtable.count((*iter)->label) == 0) { UndirectedGraphNode *tnode = new UndirectedGraphNode((*iter)->label); hashtable[(*iter)->label] = tnode; qu.push(*iter); } (hashtable[tnode->label])->neighbors.push_back(hashtable[(*iter)->label]); } } return copyNode; } }; int main(void) { return 0; }
相关文章推荐
- LeetCode(133) Clone Graph
- LeetCode: Clone Graph [133]
- [leetcode-133]Clone Graph(java)
- leetcode 133: Clone Graph
- Leetcode 133 Clone Graph
- leetcode_c++:图:Clone Graph(133)
- [leetcode] 133 clone graph bfs
- [leetcode] 133 Clone Graph
- leetcode——133——Clone Graph
- Java for LeetCode 133 Clone Graph
- [leetcode 133]Clone Graph
- leetcode || 133、Clone Graph
- leetcode做题总结,题目Clone Graph 133
- [LeetCode#133]Clone Graph
- 133 Clone Graph [Leetcode]
- leetcode[133]Clone Graph
- Clone Graph Leetcode 133
- LeetCode(133)Clone a Graph
- LeetCode 133 Clone Graph (BFS || DFS)
- [Leetcode 133, Medium] Clone Graph