当前位置:网站首页>有向图深拷贝
有向图深拷贝
2022-06-12 21:25:00 【围城_危城】
无向图node结点
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
方式一,深度优先遍历算法拷贝:
class Solution {
//存储访问过的结点及其拷贝的结点
private HashMap <Node, Node> visited = new HashMap <> ();
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
// 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if (visited.containsKey(node)) {
return visited.get(node);
}
// 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
Node cloneNode = new Node(node.val, new ArrayList());
// 哈希表存储
visited.put(node, cloneNode);
// 遍历该节点的邻居并更新克隆节点的邻居列表
for (Node neighbor: node.neighbors) {
cloneNode.neighbors.add(cloneGraph(neighbor));
}
return cloneNode;
}
}
方式二、广度优先遍历算法拷贝:
class Solution {
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
HashMap<Node, Node> visited = new HashMap();
// 将题目给定的节点添加到队列
LinkedList<Node> queue = new LinkedList<Node> ();
queue.add(node);
// 克隆第一个节点并存储到哈希表中
visited.put(node, new Node(node.val, new ArrayList()));
// 广度优先搜索
while (!queue.isEmpty()) {
// 取出队列的头节点
Node n = queue.remove();
// 遍历该节点的邻居
for (Node neighbor: n.neighbors) {
if (!visited.containsKey(neighbor)) {
// 如果没有被访问过,就克隆并存储在哈希表中
visited.put(neighbor, new Node(neighbor.val, new ArrayList()));
// 将邻居节点加入队列中
queue.add(neighbor);
}
// 更新当前节点的邻居列表
visited.get(n).neighbors.add(visited.get(neighbor));
}
}
return visited.get(node);
}
}
边栏推荐
- Teamwork collaboration application experience sharing | community essay solicitation
- Leetcode: 210. Programme II
- What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
- atoi超强解析
- Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun
- #113 Path Sum II
- C language learning notes (II)
- leetcode:210. 课程表 II
- 初步了解認識正則錶達式(Regex)
猜你喜欢

一款高颜值的MySQL管理工具

GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28

Data visualization - histogram

Teamwork collaboration application experience sharing | community essay solicitation

Pixel level reconstruction and restoration technology to solve severe image blur

GNS安装与配置

Introduction to the characteristics of balancer decentralized exchange market capitalization robot

GNS installation and configuration

Integrated monitoring solution for power environment of small and medium-sized computer rooms

C language learning notes (II)
随机推荐
测试基础之:单元测试
ASCII 码对照表
Shell script Basics
指针与数组&指针与const&结构体与const
Pytoch distributed training error
Select sort
Understanding of closures
torch. Finfo function
Data visualization - biaxial comparison effect
leetcode:207. Class Schedule Card
What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
A blog written clearly by vit
Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
Ubuntu16.04 completely delete MySQL database
中小型机房动力环境综合监控解决方案
#113 Path Sum II
Pixel level reconstruction and restoration technology to solve severe image blur
USB机械键盘改蓝牙键盘
Scatter in pytorch_ () function
Principales étapes de la collecte des ordures à Zgc