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. CentOS安装系统时硬盘分区建议

      一.常见挂载点的情况说明一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区),其中,/ 是必须的: 详细内容见下文: 安装系统时选择creat custom ...

  2. 17. Jmeter-取样器一

    jmeter-sampler介绍与使用 HTTP请求 Test Action Debug Sampler AJP/1.3 Sampler Access Log Sampler BeanShell Sa ...

  3. 微信小程序发送红包功能。填坑记录

    微信官方文档 1.开通条件 (1)商户号已入驻90日 (2)商户号有连续30天正常交易 (3)只有企业资质的商户才有资格申请 2.注意事项 (1)目前小程序红包仅支持用户微信扫码打开小程序 (2)小程 ...

  4. git 资料

    git学习资料整理(知乎搜集的) https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...

  5. vscode 常用命令行

    Ctrl+Shift+P:  打开命令面板 打开一个新窗口: Ctrl+Shift+N  关闭窗口: Ctrl+Shift+W 新建文件 Ctrl+N 代码行缩进 Ctrl+[ . Ctrl+] 上下 ...

  6. docker概述与安装及运行容器

    传统虚拟化 传统虚拟化步骤 1.安装虚拟化软件以及虚拟化的管理软件 2.创建虚拟机 3.给虚拟机安装os 4.在虚拟机内部不是应用(http.db之类的应用) 传统虚拟化的特点 1.VM与VM之间是完 ...

  7. aiohttp上报:Got more than 8190 bytes (10160) when reading Status line is too long.错误的解决办法

    通过浏览器向web服务传递base64码的图片时遇到参数过长的问题? 解决办法:查看aiohttp的源码:aiohttp/http_parser.py下找到: class HeadersParser: ...

  8. Idea maven项目不能新建package和class的解决【转】

    如图,新建的maven项目不能新建package 这是因为java是普通的文件夹,要设置为 现在就可以了 博客原链接:http://blog.csdn.net/qq_24949727/article/ ...

  9. Django Rest框架 流程详解

    什么是Restful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类审 ...

  10. Dubbox本地 JAR包部署与安装

    Dubbox的jar包并没有部署到Maven的中央仓库中,大家在Maven的中央仓库中可以查找到Dubbo的最终版本是2.5.3 , 阿里巴巴解散了Dubbo团队后由当当网继续维护此项目,并改名为 D ...