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. PHP二维数组按某个键值排序

    $data=Array(    [0] => Array        (            [id] => 2            [user_id] => 14       ...

  2. Python 3基础教程19-模块导入语法

    本文开始介绍模块导入的一些基本语法,我们现在还在Python自带的IDLE编辑器里写Python代码,如果你要需要一个功能,例如build-in的模块,那么你就需要先导入这个模块,然后才能使用这个模块 ...

  3. Python网络编程(线程通信、GIL、服务器模型)

    什么是进程.进程的概念? 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的地址空间, 一般情况下,包括文本区域(text region).数据区域(data region)和堆栈( ...

  4. spring boot 打包问题

    一.jar包 1.maven build package 2.linux 下执行 java -jar & 命令后台运行,也可加入服务运行 二.war包 1.将pom中的<packagin ...

  5. [Effective Python] 用Pythonic方式来思考

    Effective Python chap.1 用Pythonic方式来思考 Pythonic: 一门语言的编程习惯是由用户来确立的. 1. 确认自己所使用的Python版本 2. 遵循PEP8风格指 ...

  6. 【UML】类图介绍

    1.类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 2.类的关系有泛化(Generalization). ...

  7. [剑指Offer] 26.二叉搜索树与双向链表

    [思路]因为二叉搜索树的中序遍历就是递增排列的,所以只要在中序遍历时将每个结点放入vector中,再分别为每个结点的左右指针赋值即可. /* struct TreeNode { int val; st ...

  8. Eclipse中构建scala开发环境的步骤

    Eclipse是一款非常使用的开发工具,熟悉它的童鞋应该都知道,它不仅是最常用的android开发工具,还是最常用的Java开发工具.既然eclipse如此重要,本文小编就和大家一起来扒一扒在ecli ...

  9. 关于逻辑运算符&&和||及!

    && 表示and ,|| 表示or,!表示not. And(&&):对两个Boolean表达式执行逻辑和. AndAlso(&):与AndAlso类似,关键差异 ...

  10. 【题解】[WC2006]水管局长

    感觉这题好强啊……本来以为能过,结果毫无疑问的被ge了一顿……在这里记录一下做的过程,也免得以后又忘记啦. 首先,我们应看出在这张图上,要让经过的水管最长的最短,就是要维护一棵动态的最小生成树.只是删 ...