Clone Graph题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/clone-graph/description/


Description

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

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
/ \
\_/

Solution

class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if (node == NULL)
return NULL;
map< UndirectedGraphNode*, UndirectedGraphNode* > hashmap;
queue< UndirectedGraphNode* > nodeq;
nodeq.push(node);
hashmap[node] = new UndirectedGraphNode(node -> label);
UndirectedGraphNode *curNodeHash;
while (!nodeq.empty()) {
UndirectedGraphNode* preNode = nodeq.front();
nodeq.pop();
for (auto curNode : preNode -> neighbors) {
if (hashmap.find(curNode) == hashmap.end()) {
curNodeHash = new UndirectedGraphNode(curNode -> label);
hashmap[curNode] = curNodeHash;
nodeq.push(curNode);
}
(hashmap[preNode] -> neighbors).push_back(hashmap[curNode]);
}
} return hashmap[node];
}
};

解题描述

在这道题上面还花费了很多的时间,一开始想到的算法就是BFS,但是BFS得到的结果总是WA。不断WA的过程中才想到,一个顶点可以有多条指向自己的边。我一开始是没有想到这个问题,然后使用map模拟邻接矩阵来记录边总是WA。然后到后面查阅了相关资料才发现,用哈希的方式,将原图和克隆图的每一个点一一对应起来是最为准确的,且会自动排除BFS中已经访问的点。另外,对于指向自己的边的记录,使用哈希也能准确反映其内涵:顶点包含的顶点指针vector中的顶点就是当前顶点的指针。另外,很多资料中提到,这类题使用DFS递归运算更为精妙简洁。

[Leetcode Week3]Clone Graph的更多相关文章

  1. [LeetCode] 133. Clone Graph 克隆无向图

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

  2. leetcode 133. Clone Graph ----- java

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

  3. 【leetcode】Clone Graph(python)

    类似于二叉树的三种遍历,我们能够基于遍历的模板做非常多额外的事情,图的两种遍历,深度和广度模板相同也能够做非常多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先訪问第一个结点, ...

  4. Java for LeetCode 133 Clone Graph

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

  5. [leetcode]133. Clone Graph 克隆图

    题目 给定一个无向图的节点,克隆能克隆的一切 思路 1--2 | 3--5 以上图为例, node    neighbor 1         2, 3 2         1 3         1 ...

  6. Leetcode#133 Clone Graph

    原题地址 方法I,DFS 一边遍历一边复制 借助辅助map保存已经复制好了的节点 对于原图中每个节点,如果已经复制过了,直接返回新节点的地址,如果没复制过,则复制并加入map中,接着依次递归复制其兄弟 ...

  7. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  8. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. 【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 ...

随机推荐

  1. ES6 中 export ,export default 区别

    1.export与export default均可用于导出常量.函数.文件.模块等: 2.你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对 ...

  2. fiddler之弱网测试

    今天就说一下如何使用fiddler做弱网测试 1.首先要把手机的代理打开,这就不多讲了哈,不懂得话请点传送门:https://www.cnblogs.com/fuxinxin/p/9146693.ht ...

  3. Django数据模型--字段详解

    一.字段 1.CharField: 字段数据类型为字符串 class Test(models.Model): test = models.CharField(max_length=) 2.Intege ...

  4. python学习总结---网络编程

    网络编程 相关概念 - OSI七层模型:它从低到高分别是:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. - TCP/IP: 在OSI七层模型基础上简化抽象出来的一套网络协议簇,现在得到 ...

  5. python学习总结------邮件与短信

    邮件发送 - 简介: - 邮件服务器.用户名.密码 - 相关协议: - SMTP:简单邮件传输协议 - POP3:邮局通讯协议 - IMAP:交互式邮件存取协议 - SMTP协议默认端口是25 - 用 ...

  6. BZOJ 4408 FJOI2016 神秘数 可持久化线段树

    Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 ...

  7. Daily Scrum02 12.03

    Daily Scrum03 12.03 一天过去了,新的一天即将到来,我们组仍旧干劲十足呢~ Member Today's Task Tomorrow's Task 李孟 孟神有点累了呢 task85 ...

  8. Xshell出现要继续使用此程序必须应用到最新的更新或使用新版本

    资源可以用,但是安装完成后启动会报错:“要继续使用此程序,您必须应用最新的更新或使用新版本” 解决办法先修改你电脑时间为前一年(2017 1月),然后就可以打开xshell了,打开后"工具& ...

  9. C++编码规范101

    组织和策略问题 第0条 不要拘泥于小节(又名:了解哪些东西不应该标准化) 第1条 在高警告级别干净利落地进行编译 第2条 使用自动构建系统 第3条 使用版本控制系统 第4条 在代码审查上投入 设计风格 ...

  10. http协议中到底都有什么内容?【持续更新】

    http协议中到底都会传输我电脑上的啥东西呢?主机名,账号密码? 没有主机名,有你这台主机的操作系统...也就是说他们会知道你的操作系统.....