算法作业_41(2017.6.24第十八周)
来源:互联网 发布:淘宝客服人工服务在线 编辑:程序博客网 时间:2024/05/18 17:58
133. Clone Graph
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) {}; * }; */class Solution {public: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { map<int,UndirectedGraphNode *> hashmap; return clone(node,hashmap); } private: UndirectedGraphNode *clone (UndirectedGraphNode *node,map<int,UndirectedGraphNode * > &hashmap){ if(node == NULL) return NULL; if(hashmap.find(node->label)!=hashmap.end()) return hashmap[node->label]; UndirectedGraphNode *res = new UndirectedGraphNode (node->label); hashmap[node->label] = res ; for(int i = 0 ;i <node->neighbors.size();i++){ UndirectedGraphNode *neib = clone(node->neighbors[i],hashmap); res->neighbors.push_back(neib); } return res ; }};
阅读全文
0 0
- 算法作业_41(2017.6.24第十八周)
- 算法作业_37(2017.6.19第十八周)
- 算法作业_38(2017.6.20第十八周)
- 算法作业_39(2017.6.21第十八周)
- 算法作业_40(2017.6.22第十八周)
- 算法第十八周作业01
- 第十八周作业 1.0
- 算法设计与结构基础作业第十八周
- 算法课第十八周作业 | Course Schedule II
- 算法课第十八周Same Tree
- leetcode第十八周解题总结-贪心算法
- 第十八周
- 第十八周
- 第十八周算法分析与设计:Add Two Numbers
- 算法作业_25(2017.6.1第十五周)
- 算法作业_26(2017.6.6第十六周)
- 算法作业_27(2017.6.8第十六周)
- 算法作业_28(2017.6.8第十六周)
- oracle优化-shared pool
- Kotlin-16.嵌套类/内部类(Nested Classes/Inner classes)
- Centos7服务器搭建django项目
- Xilinx FPGA 的PCIE 设计
- 通达OA工作流开罚款通过触发器回写到人事档案-奖惩模块
- 算法作业_41(2017.6.24第十八周)
- python安装redis
- GCTF2017-debug.exe
- hdu Problem-1426数独问题(dfs+回溯+枚举)
- 本地部署项目可以正常访问,放在服务器上报404
- 视音频数据处理入门:RGB、YUV像素数据处理
- AFNetworkReachabilityManager 监控网络状态(四)
- Python函数参数探索
- pycharm安装、“注册”和个性化设置