-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1206.py
123 lines (110 loc) · 3.77 KB
/
1206.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
class node(object):
def __init__(self,num):
self.val = num
self.next = None
self.before = None
self.down_state = None
self.up_state = None
class Skiplist(object):
def __init__(self):
self.head1 = node(-1)
self.head2 = node(-1)
self.head3 = node(-1)
self.head4 = node(-1)
self.head1.up_state = self.head2
self.head2.up_state = self.head3
self.head3.up_state = self.head4
self.head2.down_state = self.head1
self.head3.down_state = self.head2
self.head4.down_state = self.head3
def search(self, target):
"""
:type target: int
:rtype: bool
"""
head = self.head1
findposition = self.find_position(target,head)
if findposition.val< target:
print("search",target,"False")
return False
elif findposition.val == target:
print("search",target,"True")
return True
def find_position(self, target, head):
while head.next!=None and head.next.val <= target:
head = head.next
if head.next == None:
if head.up_state == None:
return head
return self.find_position(target,head.up_state)
else:
if head.up_state == None:
return head
return self.find_position(target,head.up_state)
def add(self, num):
"""
:type num: int
:rtype: None
"""
head = self.head1
self.add_node(num,head)
print("add",num,"Null")
def add_node(self,num,head):
while head.next!=None and head.next.val <= num:
head = head.next
if head.up_state == None:
new_node = node(num)
new_node.next = head.next
head.next = new_node
if new_node.next!= None:
new_node.next.before = new_node
new_node.before = head
return new_node
if head.up_state != None:
new_head = self.add_node(num,head.up_state)
if new_head != False:
if random.random()<0.5:
new_node = node(num)
new_node.up_state = new_head
new_head.down_state = new_node
new_node.next = head.next
head.next = new_node
if new_node.next!= None:
new_node.next.before = new_node
new_node.before = head
return new_node
else:
return False
return new_head
def erase(self, num):
"""
:type num: int
:rtype: bool
"""
head = self.head1
findposition = self.find_position(num,head)
if findposition.val!= num:
print("erase",num,"False")
return False
elif findposition.val == num:
next_traget = findposition.down_state
findposition.before.next = findposition.next
if findposition.next!= None:
findposition.next.before = findposition.before
del findposition
findposition = next_traget
while findposition!= None:
next_traget = findposition.down_state
findposition.before.next = findposition.next
if findposition.next!= None:
findposition.next.before = findposition.before
del findposition
findposition = next_traget
print("erase",num,"TRUE")
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)