今天的工作给人的压力太大,实在无法排解心中的抑郁,下班后只能继续在leetcode找乐子。于是遇到了leetcode 1206,一道hard题,虽说是hard题,但瞟一眼发现顺序容器就可以秒杀它,废话不多说,上代码
1206. 设计跳表
from sortedcontainers import SortedList
class Skiplist:
def __init__(self):
self.num1 = SortedList([])
def search(self, target: int) -> bool:
n = bisect.bisect_left(self.num1,target)
if n == len(self.num1):
return False
elif self.num1[n] == target:
return True
else:
return False
def add(self, num: int) -> None:
self.num1.add(num)
def erase(self, num: int) -> bool:
n = bisect.bisect_left(self.num1,num)
if n == len(self.num1):
return False
elif self.num1[n] == num:
self.num1.discard(num)
return True
else:
return False
# 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)
希望明天的工作压力能小点,能开心一些,为自己加油!