Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.

Example:

Input:
{"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1} Explanation:
Node 1's value is 1, and it has two neighbors: Node 2 and 4.
Node 2's value is 2, and it has two neighbors: Node 1 and 3.
Node 3's value is 3, and it has two neighbors: Node 2 and 4.
Node 4's value is 4, and it has two neighbors: Node 1 and 3.

Note:

  1. The number of nodes will be between 1 and 100.
  2. The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
  3. Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
  4. You must return the copy of the given node as a reference to the cloned graph.

Soulution:

  使用DFS和BFS即可

  两种方法在leetcode中都能通过,但在牛客中,BFS使用的而外空间超出限制,所以只能使用DFS

  

 struct UndirectedGraphNode {
int label;
vector<UndirectedGraphNode *> neighbors;
UndirectedGraphNode(int x) : label(x) {};
}; //DFS
class Solution01 {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
return helper(node, mapR);
}
UndirectedGraphNode* helper(UndirectedGraphNode*node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>&mapR)
{
if (node == nullptr)return nullptr;
if (mapR.find(node) != mapR.end())//旧节点
return mapR[node];
UndirectedGraphNode* res = new UndirectedGraphNode(node->label);//新节点
mapR[node] = res;
for (auto a : node->neighbors)
res->neighbors.push_back(helper(a, mapR));
return res;
}
};
//BFS
class Solution02 {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
queue<UndirectedGraphNode*>qNode;
qNode.push(node);
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
mapR[node] = res;
while (!qNode.empty())
{
UndirectedGraphNode *p = qNode.front();
qNode.pop();
for (auto a : p->neighbors)
{
if (mapR.find(a) == mapR.end())//新节点
{
qNode.push(a);//加入
mapR[a] = new UndirectedGraphNode(a->label);//新节点
}
mapR[p]->neighbors.push_back(mapR[a]);//连接节点
}
}
return res;
}
};

力扣算法——133.CloneGraph【M】的更多相关文章

  1. 力扣算法经典第一题——两数之和(Java两种方式实现)

    一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...

  2. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  3. C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法

    题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...

  4. 力扣算法——135Candy【H】

    老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中,评分高 ...

  5. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  6. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  7. 62 (OC)* leetCode 力扣 算法

    1:两数之和 1:两层for循环 2:链表的方式 视频解析 2:两数相加 两数相加 3. 无重复字符的最长子串 给定一个字符串,请找出其中长度最长且不含有重复字符的子串,计算该子串长度 无重复字符的最 ...

  8. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. 力扣算法:125-验证回文串,131-分割回文串---js

    LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...

随机推荐

  1. iterm2简易登录服务器

    文章目录 添加文件 添加配置 直接登录 方法一 方法二 添加文件 在mac任意目录添加 10.0.1.1.txt ,这里的名字可以随意起,也可以不是txt #!/usr/bin/expect set ...

  2. springmvc知识点整理

    1.Springmvc架构 2.Springmvc组件三大组件:处理器映射器,处理器适配器,视图解析器处理器映射器:注解式处理器映射器,对类中标记了@ResquestMapping的方法进行映射,根据 ...

  3. python让人头大的装饰器...decorator带参不带参用法和原理.,..

    0. 概念什么叫装饰器,其实也可以叫做包装器.即对于一个既有的函数func(args),在调用它之前和之后,我们希望都做一些事情,把这个函数包装起来. python中的装饰器分为两类:函数装饰器和类装 ...

  4. 18. Jmeter-取样器二

    jmeter-sampler介绍与使用 JMS Point-to-Point JMS Publisher JMS Subscriber JSR223 Sampler JUnit Request Jav ...

  5. shell ssh和mount 挂载问题

    任务: 将服务器端数据挂载在板子上 1. 首先ssh问题 spawn ssh $remote_user@$remote_host (1) ssh:connect to host 10.110.6.50 ...

  6. java虚拟机规范(se8)——class文件格式(七)

    4.7.5 Exceptions 属性 Exceptions 属性是一个变长属性,它位于 method_info(§4.6)结构的属性表中. Exceptions 属性指出了一个方法需要检查的可能抛出 ...

  7. cookie,seesion学习

    一,为什么需要cookie和session? 1,Web应用程序是使用HTTP协议传输数据的.然而HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的 ...

  8. ogeek babyrop

    拖入ida 先用strncmp使一个随机数与输入比对,这里可以用\x00跳过strncmp 然后read()中的a1是我们输入\x00后的值 写exp from pwn import * sh=rem ...

  9. Docker入门 .Net Core 使用Docker全程记录

    https://www.cnblogs.com/flame7/p/9210986.html Docker入门 第一课 --.Net Core 使用Docker全程记录   微服务架构无疑是当前最火热的 ...

  10. PL/SQL to update all columns

    undefine schema_name; declare l_Err ); begin for r in (select atc.table_name, atc.column_name, atc.d ...