当前位置:网站首页>复制带随机指针的链表[空间换时间--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
参考文献
边栏推荐
- shell-运算符
- I wonder if I can open an account today? In addition, is it safe to open an account online now?
- [LeetCode] 只出现一次的数字【136】
- 6.29日刷题题解
- Virtual machine online migration based on openstack
- 6.28日刷题题解
- 500 error occurred after importing skins folder into solo blog skin
- Is China Merchants Securities reliable? Is it safe to open a stock account?
- Divisor
- LC:有效的数独 + 旋转图像
猜你喜欢
333333333333333333333333333333
雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前
Head on Amway! Good looking and practical motor SolidWorks model material see here
Simple understanding of B tree and b+ tree
这个简单的小功能,半年为我们产研团队省下213个小时
简单理解B树和B+树
Exploration and Practice on the future direction of byte cloud database
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
[Shangshui Shuo series] day 8
随机推荐
Analysis of common vlog parameters
Exploration and Practice on the future direction of byte cloud database
LC: maximum subarray and
Official website of Greentree
【微信小程序】认识小程序项目的基本组成结构
Shell positional parameter variables and predefined variables
Embedded development: Hardware in the loop testing
Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
shell-运算符
Viewing splitchunks code segmentation from MPX resource construction optimization
Common knowledge of ECS security settings
Which securities company is good for opening a mobile account? In addition, is it safe to open a mobile account?
剑指 Offer 14- II. 剪绳子 II
Set up enterprise NTP time server
Sword finger offer 14- I. cut rope
數莓派 4怎麼樣?可能的玩法有哪些?
Sword finger offer 15 Number of 1 in binary
RRDTOOL draws MRTG log data
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
深度学习的历史