当前位置:网站首页>Leetcode:1206. design jump table [jump table board]
Leetcode:1206. design jump table [jump table board]
2022-07-26 17:27:00 【White speed Dragon King's review】


analysis
Core data structure
The random height generated by the newly added node
The core traverses from the top to the bottom
Space n, Time logn
Specific implementation reference notes
ac code
MAX_LEVEL = 32 # The most layers
P_FACTOR = 0.25 # The first i There is a node in the layer , The probability of this node on the next level is p, If there is no current layer, there must be no next layer
def random_level() -> int:
# For newly added nodes , Random its lv, Represents it [1,lv] There are pointers
lv = 1
while lv < MAX_LEVEL and random.random() < P_FACTOR:
lv += 1
return lv
# A new hop table node
class SkiplistNode:
# Property slot stuck
__slots__ = 'val', 'forward' # forward Indicates the position of the next pointer , Yes max_level layer , Please see the data structure chart for details
def __init__(self, val, max_level = MAX_LEVEL):
self.val = val
self.forward = [None] * max_level # Initialize the following pointer
class Skiplist:
def __init__(self):
# Empty meaningless header node
self.head = SkiplistNode(-1)
# There are currently no nodes ,0 layer
self.level = 0
def search(self, target: int) -> bool:
''' Each layer finds the closest target But less than target The location of , Go to see No 1 Whether the layers have equal '''
curr = self.head
# Look down from the top
for i in range(self.level - 1, -1, -1):
# Find No i The layer is smaller than and closest to target The elements of
while curr.forward[i] and curr.forward[i].val < target:
curr = curr.forward[i]
# The next one on the last floor will see if it is with target equal
curr = curr.forward[0]
return curr is not None and curr.val == target
def add(self, num: int) -> None:
''' Same as search The search method of , Use the insertion of each layer of linked list to complete '''
# update Record that each layer is less than and closest to target The elements of , For subsequent updates
update = [self.head] * MAX_LEVEL
curr = self.head
for i in range(self.level - 1, -1, -1):
# Find No i The layer is smaller than and closest to num The elements of
while curr.forward[i] and curr.forward[i].val < num:
curr = curr.forward[i]
# Record
update[i] = curr
# Determine the number of layers of new nodes
lv = random_level()
self.level = max(self.level, lv)
# Open point
new_node = SkiplistNode(num, lv)
# The linked list of each layer is updated , Insertion of linked list
for i in range(lv):
new_node.forward[i] = update[i].forward[i]
update[i].forward[i] = new_node
def erase(self, num: int) -> bool:
''' find or Did not find , Deletion of nodes in the linked list '''
# update Record that each layer is less than and closest to target The elements of , For subsequent updates
update = [self.head] * MAX_LEVEL
curr = self.head
for i in range(self.level - 1, -1, -1):
# Find No i The layer is smaller than and closest to num The elements of
while curr.forward[i] and curr.forward[i].val < num:
curr = curr.forward[i]
# Record
update[i] = curr
# The first 1 Next to the layer , See if there is num
curr = curr.forward[0]
if curr is None or curr.val != num: # non-existent
return False
# Delete node
for i in range(self.level):
if update[i].forward[i] != curr:
break
# Update from bottom to top , Right. i Update the status of the layer , take forward Point to the next hop of the deleted node
update[i].forward[i] = curr.forward[i]
# Update current level
while self.level > 1 and self.head.forward[self.level - 1] is None:
self.level -= 1 # Remove the empty layer
return True
# Your Skiplist object will be instantiated and called as such:
# obj = Skiplist()
# param_1 = obj.search(target)
# obj.add(num)
# param_3 = obj.erase(num)
summary
Skip the watch board
边栏推荐
- In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe
- API for sellers -- description of the return value of adding baby API to Taobao / tmall sellers' stores
- 【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐
- The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
- Pytorch中的tensor操作
- Detailed explanation of tcpdump command
- [classification] vgg16 training record
- [ctfshow web] deserialization
- (24)Blender源码分析之顶层菜单显示代码分析
- Current limiting comparison: how to choose sentinel vs hystrix?
猜你喜欢

How does win11 reinstall the system?

What kind of product is the Jetson nano? (how about the performance of Jetson nano)

How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections

Why are test / development programmers who are better paid than me? Abandoned by the times

The principle of reliable transmission in TCP protocol

Implementing DDD based on ABP -- aggregation and aggregation root practice

Thinkphp历史漏洞复现
![[express receives get, post, and route request parameters]](/img/6c/ac936a8dff50b803993bef5192723b.png)
[express receives get, post, and route request parameters]

Speaker recruitment | AI time recruit icml/ijcai 2022 as a Chinese speaker!!!

得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
随机推荐
Can TCP and UDP use the same port?
注意 公安部发出旅游客运交通安全预警
Oracle is slow to perform a large number of DML operations. Is it the problem of CPU or hard disk?
OA项目之我的会议(会议排座&送审)
Batch normalization batch_ normalization
正则表达式
How to connect tdengine with idea database tool?
Pack tricks
[development tutorial 8] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - triaxial meter pace
In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase
使用 replace-regexp 在行首添加序号
How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections
Are CRM and ERP the same thing? What's the difference?
[basic course of flight control development 2] crazy shell · open source formation UAV - timer (LED flight information light and indicator light flash)
API analysis of Taobao / tmall shipping address list and express delivery fees
In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe
得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
Crazy God redis notes 02
Why are test / development programmers who are better paid than me? Abandoned by the times
Heavy! Zeng Xuezhong was promoted to vice chairman and CEO of zhanrui, and Chu Qingren was appointed as co CEO!