【leetcode 133. 克隆图】解题报告
方法一:dfs(递归)
map<Node*,Node*> dict;
Node* clone(Node* node)
{
if (!node) return node;
if (dict.count(node)) return dict[node];
dict[node]=new Node(node->val,vector<Node*>{}); // 这里不能写clone(node),会导致死循环,记住,在new的时候千万不要再递归,递归最低层一定有一个明确结果,所以要把截止条件写清楚
for(auto it:node->neighbors)
dict[node]->neighbors.push_back(clone(it));
return dict[node];
}
Node* cloneGraph(Node* node) {
return clone(node);
}
方法二:dfs(非递归)
map<Node*,Node*> dict;
Node* cloneGraph(Node* node)
{
stack<Node*> S;
S.push(node);
while (!S.empty())
{
Node *p = S.top();
S.pop();
if (!dict.count(p)) // 从栈中出来的都是没有进行访问过的点
dict[p]=new Node(p->val,vector<Node*>{});
for (auto it:p->neighbors)
{
if (!dict.count(it)) // 判断是否已经访问过该点
{
dict[it]=new Node(it->val,vector<Node*>{});
S.push(it);
}
dict[p]->neighbors.push_back(dict[it]); // 将新点的拷贝放入neighbors中
}
}
return dict[node];
}
【leetcode 133. 克隆图】解题报告的更多相关文章
- Java实现 LeetCode 133 克隆图
133. 克隆图 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node]). class Node { ...
- Leetcode 133.克隆图
克隆图 克隆一张无向图,图中的每个节点包含一个 label (标签)和一个 neighbors (邻接点)列表 . OJ的无向图序列化: 节点被唯一标记. 我们用 # 作为每个节点的分隔符,用 , 作 ...
- Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)
Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...
- 【LeetCode】133. 克隆图
133. 克隆图 知识点:图:递归;BFS 题目描述 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆). 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[No ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- ES6中新添加的Array.prototype.fill
用法 array.fill(start=0, end=this.length) 示例 [1, 2, 3].fill(4) // [4, 4, 4] [1, 2, 3].fill(4, 1) // [1 ...
- opencv在64位4418上的移植
1.mkdir build 2.cmake-gui 操作系统写Linux 去掉 去掉WITH_CUDA 去掉WITH_GTK 去掉WITH_1394 去掉WITH_GSTREAMER 去掉WITH_L ...
- c语言和设计模式
在网上看到一个博客专门写了关于设计模式的文章,感觉很有用.其实,我感觉数据结构 算法 设计模式 这三样同等重要啊. 数据结构 算法相对而言接触的比较多,但设计模式这个东西真的一头雾水,有时候觉得别人写 ...
- Linux route
一.简介 Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的 ...
- [SoapUI] SoapUI命令行方式运行
https://www.soapui.org/test-automation/running-from-command-line/functional-tests.html TestRunner Co ...
- Rabbit MQ参考资料
https://github.com/ServiceStack/rabbitmq-windows/blob/master/README.md https://github.com/rabbitmq/r ...
- 白盒测试实践项目(day4)
华中科技大学教材订购系统 代码评审会议纪要 与会人员: 胡俊辉.杨瑞丰.汪鸿.张颖.李建文 评审标准: 此次代码评审会议,我们小组选用了阿里巴巴Java开发手册对代码进行评审. 会议过程: 1:周末下 ...
- [GO]并发实现聊天室服务器
package main import ( "net" "fmt" "strings" "time") type Cli ...
- [GO]使用select实现斐波那契
package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...
- left join、right join、inner join、full join
转自:某一网友 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join ...