力扣算法——133.CloneGraph【M】
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:
- The number of nodes will be between 1 and 100.
- The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
- Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
- 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】的更多相关文章
- 力扣算法经典第一题——两数之和(Java两种方式实现)
一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法
题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...
- 力扣算法——135Candy【H】
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中,评分高 ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 62 (OC)* leetCode 力扣 算法
1:两数之和 1:两层for循环 2:链表的方式 视频解析 2:两数相加 两数相加 3. 无重复字符的最长子串 给定一个字符串,请找出其中长度最长且不含有重复字符的子串,计算该子串长度 无重复字符的最 ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法:125-验证回文串,131-分割回文串---js
LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...
随机推荐
- 小程序中封装base64
function Base64() { // private property var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...
- iText例子
参考:http://itextpdf.com/book/examples.php daniel@daniel-mint ~/latex/linux/itext/daniel $ cat HelloWo ...
- Ascii Chart
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex -------------------------- ...
- luoguP1514 引水入城 题解(NOIP2010)(Bfs+贪心)
P1514 引水入城 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<c ...
- SVD和SVD++
参考自:http://blog.csdn.net/wjmishuai/article/details/71191945 http://www.cnblogs.com/Xnice/p/4522671.h ...
- .net core swagger汉化
基本swagger使用不再详解,具体百度其它帖子 1.将汉化的swagger js文件复制到项目根目录中 js代码如下 'use strict'; /** * Translator for docum ...
- 记一些经常用到的linux命令
记一些经常用到的linux命令,备忘用 用清华源pip: pip install django==1.11 tensorflow==1.4.0 keras==2.0.6 -i https://pyp ...
- DataX简介
DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hive.ADS.HBase.TableStore(O ...
- web前端架构师学习流程
JavaScript 开发(高级) 系统知识 1.1ECMAScript标准的发展过程,ES6语言对JavaScript的改进: 1.2ES6中语法层面的新特性(let.const.参数扩展.模块化等 ...
- 【彩彩只能变身队(第七组)】Alpha版
演示总结 -by 彩彩只能变身组(第七组) Part one:功能简介 教师端——班级主页 教师端——创建班级 教师端——批改作业 教师端——作业上交情况 学生端——班级主页 学生端——作业上传 在开 ...