leetcode || 133、Clone Graph
2015-04-29 10:32
405 查看
problem:
Clone an undirected graph. Each node in the graph contains a
a list of its
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use
a separator for node label and each neighbor of the node.
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
Connect node
Second node is labeled as
Connect node
Third node is labeled as
Connect node
thus forming a self-cycle.
Visually, the graph looks like the following:
Hide Tags
Depth-first Search Breadth-first
Search Graph
题意:复制图(结构和数据不变,要新建节点)
thinking:
(1)要新建图的各个节点,维持邻接关系不变。
(2)采用unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> 存储原节点和新节点。而不是unordered_map<int, UndirectedGraphNode*>,
效率要高很多
(3)采用BFS思想,将原节点的邻接节点全部入栈或堆栈,遍历节点。
(4)map中查找key是否存在可以调用find(),也可以调用count(),后者效率更高
(5)提交没通过,结果不正确:
Input:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}
Output:{0,5,1#1,5,2#2,3#3,4,4#4,5,5#5}
Expected:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}
其实,结果是正确的,因为对于无向图,节点出现的顺序不影响图的结构,只能说这个验证程序只验证了一种结果
code:
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 / \ \_/
Hide Tags
Depth-first Search Breadth-first
Search Graph
题意:复制图(结构和数据不变,要新建节点)
thinking:
(1)要新建图的各个节点,维持邻接关系不变。
(2)采用unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> 存储原节点和新节点。而不是unordered_map<int, UndirectedGraphNode*>,
效率要高很多
(3)采用BFS思想,将原节点的邻接节点全部入栈或堆栈,遍历节点。
(4)map中查找key是否存在可以调用find(),也可以调用count(),后者效率更高
(5)提交没通过,结果不正确:
Input:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}
Output:{0,5,1#1,5,2#2,3#3,4,4#4,5,5#5}
Expected:{0,1,5#1,2,5#2,3#3,4,4#4,5,5#5}
其实,结果是正确的,因为对于无向图,节点出现的顺序不影响图的结构,只能说这个验证程序只验证了一种结果
code:
class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> record; if(node == NULL) return node; stack<UndirectedGraphNode*> queue; queue.push(node); while(!queue.empty()) { UndirectedGraphNode *nextNode = queue.top(); queue.pop(); if(!record.count(nextNode)) { UndirectedGraphNode *newNode = new UndirectedGraphNode(nextNode->label); record[nextNode] = newNode; } for(int i = nextNode->neighbors.size()-1; i >= 0 ; i --) { UndirectedGraphNode *childNode = nextNode->neighbors[i]; if(!record.count(childNode)) { UndirectedGraphNode *newNode = new UndirectedGraphNode(childNode->label); record[childNode] = newNode; queue.push(childNode); } record[nextNode]->neighbors.push_back(record[childNode]); } } return record[node]; } };
相关文章推荐
- LeetCode(133) Clone Graph
- [Leetcode 133, Medium] Clone Graph
- LeetCode 133: Clone Graph
- LeetCode133 Clone Graph
- LeetCode 133 Clone Graph
- Leetcode 133 Clone Graph
- leetcode 133 clone-graph 克隆图(图的遍历算法)
- LeetCode(133) Clone Graph
- LeetCode133:Clone Graph
- LeetCode: Clone Graph [133]
- [leetcode-133]Clone Graph(java)
- leetcode 133: Clone Graph
- Leetcode 133 Clone Graph
- leetcode_c++:图:Clone Graph(133)
- Java for LeetCode 133 Clone Graph
- [leetcode] 133 clone graph bfs
- [leetcode] 133 Clone Graph
- leetcode——133——Clone Graph
- [leetcode 133]Clone Graph
- leetcode做题总结,题目Clone Graph 133