当前位置:网站首页>复制带随机指针的链表[空间换时间--hash记录]
复制带随机指针的链表[空间换时间--hash记录]
2022-06-29 23:53:00 【REN_林森】
前言
空间换时间是非常常见的降低时间复杂度做法,而hashMap/hash数组/二进制hash就是常用的方式。
一、复制带随机指针的链表

二、hash记录
package everyday.hash;
import java.util.HashMap;
import java.util.Map;
public class CopyRandomList {
public Node copyRandomList(Node head) {
// 设置dummy节点,把特殊情况head==null,以及头节点问题统一处理了。
Node dummyHead = new Node(1);
dummyHead.next = head;
Node cloneDummyHead = new Node(1);
// 记录原始节点和新生成节点映射。
Map<Node, Node> fx = new HashMap<>();
// 设置好头节点与游走节点。
Node p = dummyHead.next;
Node cur = cloneDummyHead.next;
// 设置next
while (p != null) {
// 生成新节点
Node next = new Node(p.val);
// 记录新旧节点映射,为后面设置random指针做准备。
fx.put(p, next);
// 设置新链表next
cur.next = next;
// 更新cur/p
cur = cur.next;
p = p.next;
}
// 设置random
p = dummyHead.next;
cur = cloneDummyHead.next;
while (p != null) {
// 得到cur节点random指向。
Node random = fx.get(p.random);
// 设置random
cur.random = random;
// 更新cur/p
cur = cur.next;
p = p.next;
}
// 返回克隆好的链表
return cloneDummyHead.next;
}
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
}
总结
1)空间换时间,hashMap/hashSet/hash数组/二进制hash
参考文献
边栏推荐
- LC: effective Sudoku + rotating image
- Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils
- 西门子低代码 9.14版本: 满足不同需求
- Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale
- [译]在软件开发行业工作 6 年后,那些年我曾改过的观念
- Leetcode (680) -- verifying palindrome string II
- The concept and significance of mean, variance, standard deviation and covariance
- New CorelDRAW technical suite2022 latest detailed function introduction
- Exploration and Practice on the future direction of byte cloud database
- What is IGMP? What is the difference between IGMP and ICMP?
猜你喜欢

漫画安全HIDS、EDR、NDR、XDR

333333333333333333333333333333

Head on Amway! Good looking and practical motor SolidWorks model material see here

新钛云服荣膺“2022爱分析 · IT运维厂商全景报告”云管理平台CMP 代表厂商!...

Et la tarte aux framboises 4? Quels sont les jeux possibles?

After crossing, she said that the multiverse really exists

Matplotlib plt Hist() parameter explanation

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
![[wechat applet] understand the basic composition of the applet project](/img/71/98894fbb9cda4facfd2b83c8ec8f9a.png)
[wechat applet] understand the basic composition of the applet project

Leetcode(76)——最小覆盖子串
随机推荐
Cartoon security HIDS, EDR, NDR, XDR
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
Matplotlib histogram of Matplotlib visualization plt bar()
MySQL functions and constraints
@Scheduled annotation pit, I stepped on it for you
Is China Merchants Securities reliable? Is it safe to open a stock account?
招商证券靠谱吗?开股票账户安全吗?
What is online account opening? In addition, is it safe to open a mobile account?
modelsim的TCL脚本的define incdir命令解析
Solr basic operation 2
25 interview questions about Apache
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
FPGA Development (1) -- serial port communication
6.29日刷题题解
Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
Applet plug-in access, development and precautions
Koa2 learning and using
Why is JSX syntax so popular?
LC: maximum subarray and
Golang泛型的巧妙应用,防止变量空指针错误,防止结构体字段空指针错误