leetcode 133. Clone Graph
2016-03-25 22:33
375 查看
Clone an undirected graph. Each node in the graph contains a
a list of its
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
遍历即可
accepted
labeland
a list of its
neighbors.
struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
};
遍历即可
class Solution { public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) return NULL; vector<UndirectedGraphNode*>que,nodelist,newnonelist; que.push_back(node); nodelist.push_back(node); while (!que.empty()) { vector<UndirectedGraphNode*>newque; for (int i = 0; i < que.size(); i++) for (int j = 0; j < que[i]->neighbors.size(); j++) { if (find(nodelist.begin(), nodelist.end(), que[i]->neighbors[j]) == nodelist.end()) { nodelist.push_back(que[i]->neighbors[j]); newque.push_back(que[i]->neighbors[j]); } } que = newque; } for (int i = 0; i < nodelist.size(); i++) { UndirectedGraphNode*n = new UndirectedGraphNode(nodelist[i]->label); newnonelist.push_back(n); } for (int i = 0; i < nodelist.size(); i++) { newnonelist[i]->neighbors.clear(); for (int j = 0; j < nodelist[i]->neighbors.size(); j++) { int index = find(nodelist.begin(), nodelist.end(), nodelist[i]->neighbors[j]) - nodelist.begin(); newnonelist[i]->neighbors.push_back(newnonelist[index]); } } return newnonelist[0]; } };
accepted
相关文章推荐
- 大数相乘,结果在2000位以内
- PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
- ORACLE多表关联UPDATE 语句
- Java 输出流中的flush方法
- OD使用心得
- asp.net 基礎部分一
- Linux中mv重命名作用及打包war压缩文件及分配权限
- flask-sqlalchemy(1)
- websphere中解决一个NoClassFoundException问题
- 用迪杰斯特拉算法求赋权图中的最短路径
- 一种文库类网站建设思路.md
- Spring_Q_collect
- svn的merge使用例子
- 【JAVA开发之IO流1-3】IO流之缓冲流
- N
- 猜数字游戏
- 求赋权图中一个结点到所有结点的最短路径的长度
- HTML5与CSS3基础(二)
- 《写给亲爱的你》
- 自动生成格雷码