当前位置:网站首页>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);
}
}
};
边栏推荐
- Introduction to paddle - using lenet to realize image classification method I in MNIST
- What is load balancing? How does DNS achieve load balancing?
- Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
- 8道经典C语言指针笔试题解析
- Experience of autumn recruitment in 22 years
- 5g NR system messages
- Cause analysis and solution of too laggy page of [test interview questions]
- Codeforces Round #804 (Div. 2)(A~D)
- They gathered at the 2022 ecug con just for "China's technological power"
- 51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
猜你喜欢
Reptile practice (VIII): reptile expression pack
CVE-2022-28346:Django SQL注入漏洞
C # generics and performance comparison
Reentrantlock fair lock source code Chapter 0
Fofa attack and defense challenge record
8道经典C语言指针笔试题解析
[note] common combined filter circuit
新库上线 | CnOpenData中华老字号企业名录
They gathered at the 2022 ecug con just for "China's technological power"
3.MNIST数据集分类
随机推荐
NTT template for Tourism
Service Mesh的基本模式
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
Redis, do you understand the list
Handwriting a simulated reentrantlock
fabulous! How does idea open multiple projects in a single window?
Image data preprocessing
C # generics and performance comparison
基于人脸识别实现课堂抬头率检测
New library online | information data of Chinese journalists
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
华泰证券官方网站开户安全吗?
NVIDIA Jetson测试安装yolox过程记录
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
Kubernetes static pod (static POD)
8道经典C语言指针笔试题解析
炒股开户怎么最方便,手机上开户安全吗
13.模型的保存和载入