当前位置:网站首页>Digraph deep copy
Digraph deep copy
2022-06-12 21:34:00 【Besieged city_ city with high walls】
Undirected graph node 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;
}
}
Mode one , Depth first traversal algorithm copy :
class Solution {
// Store the visited nodes and their copies
private HashMap <Node, Node> visited = new HashMap <> ();
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
// If the node has been accessed , The corresponding clone node is directly extracted from the hash table and returned to
if (visited.containsKey(node)) {
return visited.get(node);
}
// Clone node , Notice that we won't clone the list of its neighbors for deep copy
Node cloneNode = new Node(node.val, new ArrayList());
// Hash table storage
visited.put(node, cloneNode);
// Traverse the neighbors of the node and update the neighbor list of the clone node
for (Node neighbor: node.neighbors) {
cloneNode.neighbors.add(cloneGraph(neighbor));
}
return cloneNode;
}
}
Mode two 、 Breadth first traversal algorithm copy :
class Solution {
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
HashMap<Node, Node> visited = new HashMap();
// Add the node given the topic to the queue
LinkedList<Node> queue = new LinkedList<Node> ();
queue.add(node);
// Clone the first node and store it in the hash table
visited.put(node, new Node(node.val, new ArrayList()));
// Breadth first search
while (!queue.isEmpty()) {
// Take out the head node of the queue
Node n = queue.remove();
// Traverse the neighbors of the node
for (Node neighbor: n.neighbors) {
if (!visited.containsKey(neighbor)) {
// If you haven't been interviewed , Just clone and store it in the hash table
visited.put(neighbor, new Node(neighbor.val, new ArrayList()));
// Add neighbor nodes to the queue
queue.add(neighbor);
}
// Update the neighbor list of the current node
visited.get(n).neighbors.add(visited.get(neighbor));
}
}
return visited.get(node);
}
}
边栏推荐
- EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
- ASCII 码对照表
- KDD2022 | GraphMAE:自监督掩码图自编码器
- JUC并发工具包使用指南
- Icml2022 | Galaxy: apprentissage actif des cartes de polarisation
- SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
- shell语言
- Lintcode:127. Topology sorting
- Shell script Basics
- Smart management of green agriculture: a visual platform for agricultural product scheduling
猜你喜欢
Graphics2D类基本使用
一款高颜值的MySQL管理工具
Risk control modeling X: Discussion on problems existing in traditional modeling methods and Exploration on improvement methods
KDD2022 | GraphMAE:自监督掩码图自编码器
结构体知识点all in
RestTemplate的@LoadBalance注解
一级指针&二级指针知识点梳理
Experiment 7-2-6 print Yanghui triangle (20 points)
Distributed cloud service developer'allegro Xile technology 'received an angel round financing of US $3million
Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
随机推荐
NIO使用指南
冒泡排序
NPOI 创建Word
To delete a character from a string
Fill in the checklist & lt; int&gt; Have default values? [repeat] - fill list & lt; int&gt; with default values? [duplicate]
Can tonghuashun open an account? Can the security of securities companies be directly opened on the app? How to open an account for securities accounts
leetcode:210. 课程表 II
一款高颜值的MySQL管理工具
Insert sort
同花顺能开户吗,在APP上可以直接开通券商安全吗
EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
makefile 的ifeq,filter,strip 简单使用
torch. Finfo function
ORM implements the mapping relationship between classes and tables, class attributes and fields
结构体知识点all in
Two sentences to clarify JS throttling and anti shake
Ubuntu16.04 completely delete MySQL database
插入排序
SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
leetcode:207. Class Schedule Card