LeetCode OJ - Clone Graph
来源:互联网 发布:网络黑市交易论坛 编辑:程序博客网 时间:2024/05/17 22:11
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 node0
to both nodes1
and2
. - Second node is labeled as
1
. Connect node1
to node2
. - Third node is labeled as
2
. Connect node2
to node2
(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) {};
* };
*/
错误分析:此题和带random指针链表的深度拷贝类似,采用Hash表来映射,记录新旧节点的映射关系。第一遍遍历旧图新建节点和Hash表,第二遍遍历新图使用Hash表替换新的连接关系。
再分析:上述分析将图的遍历想成链表的遍历,图的遍历应考虑BFS或的DFS
BFS算法如下:(使用队列)
void BFS(graphNode *node) { queue<graphNode *>que; que.push(node); while(!que.empty()) { tmp = que.front(); que.pop(); if(tmp.isvisited()) continue; for(int i = 0; i < tmp->neighbors.size(); i++) { que.push(tmp->neighbors[i]); } }}
DFS算法如下:(使用栈)
1.入栈条件:栈顶节点的第一个未被访问的节点 2.出栈条件:无邻节点、邻节点访问完了。
void DFS(graphNode *node) { stack<graphNode *>st; st.push(node); while(!st.empty()) { tmp = st.top(); for(x in tmp->neighbors) { if(!x.isvisited()) break; } if(x == null) { //无邻节点、邻节点访问完 st.pop(); } else { //第一个未被访问的邻节点 st.push(x); output(x); } }}
采用BFS进行两边遍历:
/** * 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;map<UndirectedGraphNode *, UndirectedGraphNode *> nodeMap;queue<UndirectedGraphNode *>q;q.push(node);while(!q.empty()) {UndirectedGraphNode *cur = q.front();q.pop();//cur节点没有被访问if(nodeMap.find(cur) == nodeMap.end()) {//创建新节点UndirectedGraphNode *born = new UndirectedGraphNode(cur->label);//创建映射关系nodeMap.insert(pair<UndirectedGraphNode *, UndirectedGraphNode *>(cur, born));for(int i = 0; i < cur->neighbors.size(); i++) {q.push(cur->neighbors[i]);}}} q.push(node);while(!q.empty()) {UndirectedGraphNode *cur = q.front();q.pop();UndirectedGraphNode *newNode = nodeMap[cur];if(newNode ->neighbors.empty()) {for(int i = 0; i < cur->neighbors.size(); i++) {newNode->neighbors.push_back( nodeMap[ cur->neighbors[i] ]);q.push(cur->neighbors[i]);}}}return nodeMap[node];}};
- LeetCode OJ:Clone Graph
- LeetCode OJ - Clone Graph
- LeetCode OJ Clone Graph
- LeetCode[Graph]: Clone Graph
- [LeetCode] CLone Graph
- LeetCode:Clone Graph
- [leetcode]Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- LeetCode题解:Clone Graph
- [LeetCode]Clone Graph
- leetcode Clone Graph
- leetcode-Clone Graph
- LeetCode | Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- Leetcode Clone Graph
- [Leetcode] Clone Graph (Java)
- 《编程珠玑》——学习历程之一(利用位图(位矢量)的思想实现神奇排序)
- spring mvc 与velocity配置
- spring security 二中使用通过自定义过滤器实现多登录页面
- 字典树的使用
- 一次误操作引起的分区表恢复记
- LeetCode OJ - Clone Graph
- No29栈的push、pop序列(栈)
- Hadoop实战-中高级一部分 之 Hadoop作业调优参数调整及原理
- 函数名&函数名取地址
- ios开发答疑录系列---(七)IOS 线程处理
- 深度学习word2vec笔记之基础篇
- Ubuntu上使用另一个java IDE-->ItelliJ
- 为什么URL在浏览器能访问,放到微信上却不能访问?
- jQuery实现CheckBox全选、全不选