leetcode 133: Clone Graph
2015-08-17 22:13
585 查看
Not a hard problem, just the details are a little tricky to think. I used the queue to BFS, while doing it, I used the unordered set and unordered map to save the nodes that are already been created. Whenever a node is created before, you shouldn't create
a new one with the same label.
/**
* 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;
queue<UndirectedGraphNode*> qold;//queue for the original graph
queue<UndirectedGraphNode*> qnew;//queue for the new graph
unordered_set<UndirectedGraphNode*> oldset;//save all nodes that are existed
unordered_map<int,UndirectedGraphNode*> newset;
qold.push(node);
UndirectedGraphNode* pbegin=new UndirectedGraphNode(node->label);
qnew.push(pbegin);
while(!qold.empty())
{
UndirectedGraphNode* pold=qold.front();
qold.pop();
UndirectedGraphNode* pnew=qnew.front();
qnew.pop();
if(oldset.find(pold)==oldset.end())//make this node existed if not
{
oldset.insert(pold);
newset.insert(make_pair(pnew->label,pnew));
}
for(int i=0;i<pold->neighbors.size();i++)
{
if(oldset.find(pold->neighbors[i])!=oldset.end())//the neighbor i is an existed node
{
UndirectedGraphNode* ptemp=newset[pold->neighbors[i]->label];//find the existed node's address through label
pnew->neighbors.push_back(ptemp);
}
else
{
UndirectedGraphNode* ptemp=new UndirectedGraphNode(pold->neighbors[i]->label);
pnew->neighbors.push_back(ptemp);
qold.push(pold->neighbors[i]);
qnew.push(ptemp);
oldset.insert(pold->neighbors[i]);
newset.insert(make_pair(pold->neighbors[i]->label,ptemp));
}
}
}
return pbegin;
}
};
a new one with the same label.
/**
* 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;
queue<UndirectedGraphNode*> qold;//queue for the original graph
queue<UndirectedGraphNode*> qnew;//queue for the new graph
unordered_set<UndirectedGraphNode*> oldset;//save all nodes that are existed
unordered_map<int,UndirectedGraphNode*> newset;
qold.push(node);
UndirectedGraphNode* pbegin=new UndirectedGraphNode(node->label);
qnew.push(pbegin);
while(!qold.empty())
{
UndirectedGraphNode* pold=qold.front();
qold.pop();
UndirectedGraphNode* pnew=qnew.front();
qnew.pop();
if(oldset.find(pold)==oldset.end())//make this node existed if not
{
oldset.insert(pold);
newset.insert(make_pair(pnew->label,pnew));
}
for(int i=0;i<pold->neighbors.size();i++)
{
if(oldset.find(pold->neighbors[i])!=oldset.end())//the neighbor i is an existed node
{
UndirectedGraphNode* ptemp=newset[pold->neighbors[i]->label];//find the existed node's address through label
pnew->neighbors.push_back(ptemp);
}
else
{
UndirectedGraphNode* ptemp=new UndirectedGraphNode(pold->neighbors[i]->label);
pnew->neighbors.push_back(ptemp);
qold.push(pold->neighbors[i]);
qnew.push(ptemp);
oldset.insert(pold->neighbors[i]);
newset.insert(make_pair(pold->neighbors[i]->label,ptemp));
}
}
}
return pbegin;
}
};
相关文章推荐
- VMware安装linux虚拟系统如何链接外网?
- Android软件安全与逆向分析-笔记(1)
- Makefile 中:= ?= += =的区别
- $.ajax()详解
- 模板方法模式
- java面试中长被问到的细节
- ACM 3.2 素数筛选法
- 英文操作系统中中文乱码(SQL中 NVARCHAR 和 VARCHAR区别)
- 第三章 web设计基本概念——扩展阅读
- HDU 1226 BFS
- Java Servlet【3】<load-on-startup>的配置、ServletConfig对象如何提起信息
- Bus Pass BFS搜索
- iOS开发网络篇-JSON文件的解析
- jquery设计思想之写法-方法函数化&链式操作
- Example of MPI_Type_struct
- OC协议protocol详解
- View视图绘制流程,View工作原理(二)
- wince系统中对nand坏块的修正
- 2.8 寻找符合条件的数
- Dream------scala--类的属性和对象私有字段实战详解