当前位置:网站首页>[sword finger offer] sword finger offer 35 Replication of complex linked list
[sword finger offer] sword finger offer 35 Replication of complex linked list
2022-06-12 21:52:00 【Caicai 2022】
subject
Code
Execution time :48 ms, In all Python3 Defeated in submission 18.68% Users of
Memory consumption :15.7 MB, In all Python3 Defeated in submission 73.78% Users of
Pass the test case :18 / 18
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
dic = {
}
# 3. Copy each node , And establish “ Original node -> New node ” Of Map mapping
cur = head
while cur:
dic[cur] = Node(cur.val)
cur = cur.next
cur = head
# 4. Building new nodes next and random Point to
while cur:
dic[cur].next = dic.get(cur.next)
dic[cur].random = dic.get(cur.random)
cur = cur.next
# 5. Return the head node of the new linked list
return dic[head]
【 Method 2】
Execution time :32 ms, In all Python3 Defeated in submission 94.64% Users of
Memory consumption :15.7 MB, In all Python3 Defeated in submission 73.78% Users of
Pass the test case :18 / 18
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
cur = head
while cur:
newNode=Node(cur.val)
newNode.next=cur.next
cur.next=newNode
cur=newNode.next
cur=head
while cur:
if cur.random:
cur.next.random=cur.random.next
cur=cur.next.next
ans=cur=head.next
pre=head
while cur.next:
pre.next=pre.next.next
cur.next=cur.next.next
pre=pre.next
cur=cur.next
pre.next=None
return ans
边栏推荐
- Oracle livelabs experiment: introduction to Oracle Spatial
- Xingda easy control ModbusRTU to modbustcp gateway
- The 2023 campus recruitment officially opened! Oceanbase would like to make an interview with you this spring
- Ansible roles project case (IV)
- June training (day 10) - bit operation
- How to implement a simple publish subscribe mode
- 复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
- Oracle LiveLabs实验:Introduction to Oracle Spatial
- June training (day 11) - matrix
- Data batch writing
猜你喜欢

Xingda easy control ModbusRTU to modbustcp gateway

PCB package download website recommendation and detailed usage

SQL调优指南笔记9:Joins

关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法

Recommended Chinese font in the code input box of Oracle SQL developer

The Post-00 financial woman with a monthly salary of 2W conquered the boss with this set of report template

ICML2022 | GALAXY:极化图主动学习

Okio source code analysis

SQL调优指南笔记13:Gathering Optimizer Statistics

Ansible summary (VI)
随机推荐
MySQL介绍和安装(一)
VagrantBox重新安装vboxsf驱动
Semester summary of freshman year
ORM implements the mapping relationship between classes and tables, class attributes and fields
Producer consumer model under multithreading model
数据批量写入
SQL tuning guide notes 15:controlling the use of optimizer statistics
Ansible playbook和变量(二)
Oracle 19c 安装文档
【QNX Hypervisor 2.2 用户手册】4.3 获取host组件
My struggle: my years in foreign enterprises (1)
【目标检测】|Dive Deeper Into Box for Object Detection 基于FCOS新训练方法
MySQL master-slave replication
SQL调优指南笔记13:Gathering Optimizer Statistics
孙老师版本JDBC(2022年6月12日21:34:25)
The 2023 campus recruitment officially opened! Oceanbase would like to make an interview with you this spring
六月集训(第12天) —— 链表
Oracle LiveLabs实验:Introduction to Oracle Spatial Studio
Turing prize winner: what should I pay attention to if I want to succeed in my academic career?
阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds