当前位置:网站首页>133. 克隆图
133. 克隆图
2022-07-07 23:18:00 【anieoo】
原题链接:133. 克隆图
solution:
dfs建立原点到克隆点的映射,在遍历一遍即可
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
unordered_map<Node *, Node *> hash;
Node* cloneGraph(Node* node) {
if(node == NULL) return node;
dfs(node); //建立映射
for(auto [s, d] : hash) {
for(auto ver : s->neighbors) {
d->neighbors.push_back(hash[ver]);
}
}
return hash[node];
}
//dfs用来实现原来的点到克隆点的映射
void dfs(Node *root) {
hash[root] = new Node(root->val);
for(auto ver : root->neighbors) {
if(hash[ver] == 0)
dfs(ver);
}
}
};边栏推荐
- "An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
- 攻防演练中沙盘推演的4个阶段
- What has happened from server to cloud hosting?
- Class head up rate detection based on face recognition
- [note] common combined filter circuit
- 新库上线 | CnOpenData中国星级酒店数据
- 赞!idea 如何单窗口打开多个项目?
- Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
- Handwriting a simulated reentrantlock
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
猜你喜欢

基于微信小程序开发的我最在行的小游戏

【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础

51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

6.Dropout应用

《因果性Causality》教程,哥本哈根大学Jonas Peters讲授

取消select的默认样式的向下箭头和设置select默认字样

jemter分布式

Binder core API

Cancel the down arrow of the default style of select and set the default word of select

语义分割模型库segmentation_models_pytorch的详细使用介绍
随机推荐
牛客基础语法必刷100题之基本类型
Password recovery vulnerability of foreign public testing
[note] common combined filter circuit
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
攻防演练中沙盘推演的4个阶段
6.Dropout应用
德总理称乌不会获得“北约式”安全保障
Class head up rate detection based on face recognition
Basic principle and usage of dynamic library, -fpic option context
letcode43:字符串相乘
What if the testing process is not perfect and the development is not active?
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
手写一个模拟的ReentrantLock
13.模型的保存和載入
FOFA-攻防挑战记录
Introduction to paddle - using lenet to realize image classification method II in MNIST
A network composed of three convolution layers completes the image classification task of cifar10 data set
Cve-2022-28346: Django SQL injection vulnerability
Prediction of the victory or defeat of the League of heroes -- simple KFC Colonel