当前位置:网站首页>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);
}
}
};
边栏推荐
- 韦东山第二期课程内容概要
- Deep dive kotlin synergy (XXII): flow treatment
- 基于卷积神经网络的恶意软件检测方法
- 13. Enregistrement et chargement des modèles
- 利用GPU训练网络模型
- The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
- 14.绘制网络模型结构
- STL--String类的常用功能复写
- LeetCode刷题
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
猜你喜欢
New library online | cnopendata China Star Hotel data
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
jemter分布式
Binder core API
What has happened from server to cloud hosting?
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
CVE-2022-28346:Django SQL注入漏洞
Service Mesh介绍,Istio概述
Cve-2022-28346: Django SQL injection vulnerability
DNS series (I): why does the updated DNS record not take effect?
随机推荐
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
[reprint] solve the problem that CONDA installs pytorch too slowly
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
Cancel the down arrow of the default style of select and set the default word of select
C # generics and performance comparison
赞!idea 如何单窗口打开多个项目?
Letcode43: string multiplication
Is it safe to open an account on the official website of Huatai Securities?
13. Enregistrement et chargement des modèles
v-for遍历元素样式失效
Invalid V-for traversal element style
Experience of autumn recruitment in 22 years
They gathered at the 2022 ecug con just for "China's technological power"
German prime minister says Ukraine will not receive "NATO style" security guarantee
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
14.绘制网络模型结构
基于卷积神经网络的恶意软件检测方法
韦东山第三期课程内容概要