题目:

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.

How we serialize an undirected graph:

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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

   1
/ \
/ \
0 --- 2
/ \
\_/

Example

return a deep copied graph.

题解:

  DFS(recursion)

Solution 1 ()

class Solution {
public:
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> hash;
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (!node) return node;
if(hash.find(node) == hash.end()) {
hash[node] = new UndirectedGraphNode(node -> label);
for (auto neighbor : node -> neighbors) {
(hash[node] -> neighbors).push_back( cloneGraph(neighbor) );
}
}
return hash[node];
}
};

  DFS(stack)

Solution 2 ()

class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
stack<UndirectedGraphNode* > stack; map<int, UndirectedGraphNode* > visitTable;
UndirectedGraphNode* newnode = new UndirectedGraphNode(node->label);
visitTable[node->label] = newnode;
stack.push(node); while (!stack.empty()) {
UndirectedGraphNode* cur = stack.top();
stack.pop();
for (auto neighbor : cur->neighbors) {
if (visitTable.find(neighbor->label) == visitTable.end()) {
stack.push(neighbor);
UndirectedGraphNode* newneighbor = new UndirectedGraphNode(neighbor->label);
visitTable[neighbor->label] = newneighbor;
}
visitTable[cur->label]->neighbors.push_back(visitTable[neighbor->label]);
}
} return newnode;
}
};

  BFS

Solution 3 ()

class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == nullptr)
return nullptr;
queue<UndirectedGraphNode* > queue; map<int, UndirectedGraphNode* > visitTable;
UndirectedGraphNode* newnode = new UndirectedGraphNode(node->label);
visitTable[node->label] = newnode;
queue.push(node); while (!queue.empty()) {
UndirectedGraphNode* cur = queue.front();
queue.pop();
for (auto neighbor : cur->neighbors) {
if (visitTable.find(neighbor->label) == visitTable.end()) {
UndirectedGraphNode* newneighbor = new UndirectedGraphNode(neighbor->label);
visitTable[neighbor->label] = newneighbor;
queue.push(neighbor);
}
visitTable[cur->label]->neighbors.push_back(visitTable[neighbor->label]);
}
} return newnode;
}
};

【Lintcode】137.Clone Graph的更多相关文章

  1. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  2. 【leetcode】133. Clone Graph

    题目如下: Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph con ...

  3. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  5. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  6. 【Git】git clone报错 git fatal: Unable to find remote helper for 'https'

    [参考资料] https://stackoverflow.com/questions/8329485/unable-to-find-remote-helper-for-https-during-git ...

  7. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  8. 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification

    论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...

  9. 【POJ】【2125】Destroying the Graph

    网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...

随机推荐

  1. PHP如何识别系统语言或浏览器语言

    preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $lang = $matches[1]; switc ...

  2. rst2pdf 中文

    上篇说到用pandoc转换为reST为pdf是使用LaTeX作为中间格式的,而今天要说的rst2pdf貌似是直接转换为pdf的. 安装和调用 rst2pdf目前只支持Python2.7,因此在创建vi ...

  3. HDFS源码分析数据块复制监控线程ReplicationMonitor(一)

    ReplicationMonitor是HDFS中关于数据块复制的监控线程,它的主要作用就是计算DataNode工作,并将复制请求超时的块重新加入到待调度队列.其定义及作为线程核心的run()方法如下: ...

  4. 02 redis通用命令操作

    set hi hello 设置值 get hi 获取值 keys * 查询出所有的key memcached 不能查询出所有的key keys *h 模糊查找key keys h[ie] 模糊查找 k ...

  5. iOS 键盘变中文

    plist文件添加 Localizations 添加一项字段Chinese (simplified)

  6. TP框架---thinkphp使用ajax

    thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 一.thinkphp使用ajax返回数 ...

  7. Ajax的跨域问题

    •跨域问题概述 •出于安全考虑,浏览器不允许ajax跨域获取数据 •可以通过script的src加载js的方式传递数据 fn({"a":"1","b& ...

  8. PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)

    sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...

  9. Spring项目中使用jackson序列化key为对象Map

    1.注入ObjectMapper2.注册类HistoricTaskInstance的序列化和反序列化类HistoricTaskInstanceKeySerializer,HistoricTaskIns ...

  10. 使用 Spring 容器管理 Filter

    当我们用Filter时,往往需要使用一些辅助的service,在普通的java中,只要声明(set,get方法)后在spring-application配置文件中配置就可以了,但是由于Filter与L ...