基础题队列933、225、622、641
1 class RecentCounter: 2 3 def __init__(self): 4 self.q = collections.deque() 5 6 def ping(self, t: int) -> int: 7 self.q.append(t) 8 9 while self.q[0] < t - 3000: 10 self.q.popleft() 11 12 return len(self.q) 13 14 15 16 # Your RecentCounter object will be instantiated and called as such: 17 # obj = RecentCounter() 18 # param_1 = obj.ping(t)
popleft(),append()
1 class MyStack: #用一个队列实现栈 2 3 def __init__(self): 4 self.q = deque() 5 6 def push(self, x: int) -> None: #加元素 7 self.q.append(x) 8 9 10 def pop(self) -> int: 11 if self.empty(): 12 return None 13 14 #将len-1个元素重新append 15 for i in range(len(self.q)-1): 16 self.q.append(self.q.popleft()) 17 18 return self.q.popleft() 19 20 def top(self) -> int: #只需要获取,不需要弹出 21 #为空直接返回空 22 if self.empty(): 23 return None 24 25 return self.q[-1] 26 27 def empty(self) -> bool: 28 return not self.q #为空返回True 29 30 31 # Your MyStack object will be instantiated and called as such: 32 # obj = MyStack() 33 # obj.push(x) 34 # param_2 = obj.pop() 35 # param_3 = obj.top() 36 # param_4 = obj.empty()
1 class MyCircularQueue: 2 3 def __init__(self, k: int): #构造器,设置队列长度为 k 4 self.head = self.tail = None 5 self.capacity = k 6 self.size = 0 7 8 def enQueue(self, value: int) -> bool: 9 #向循环队列插入一个元素。如果成功插入则返回真。 10 if self.isFull(): 11 return False 12 13 #插入一个值为value的元素 14 node = ListNode(value) 15 16 if self.head is None: #列表中没有元素 17 self.head = node 18 self.tail = node 19 else: 20 self.tail.next = node 21 self.tail = node #移动尾指针 22 self.size += 1 #队列长加1 23 24 return True 25 26 def deQueue(self) -> bool: 27 #从循环队列中删除一个元素。如果成功删除则返回真 28 if self.isEmpty(): 29 return False 30 self.head = self.head.next #直接移动头指针 31 self.size -= 1 32 33 return True 34 35 def Front(self) -> int: 36 #从队首获取元素。如果队列为空,返回 -1 37 return -1 if self.isEmpty() else self.head.val 38 39 def Rear(self) -> int: 40 #获取队尾元素。如果队列为空,返回 -1 41 return -1 if self.isEmpty() else self.tail.val 42 def isEmpty(self) -> bool: 43 #检查循环队列是否为空 44 return self.size == 0 45 46 def isFull(self) -> bool: 47 #检查循环队列是否已满 48 return self.size == self.capacity 49 50 51 # Your MyCircularQueue object will be instantiated and called as such: 52 # obj = MyCircularQueue(k) 53 # param_1 = obj.enQueue(value) 54 # param_2 = obj.deQueue() 55 # param_3 = obj.Front() 56 # param_4 = obj.Rear() 57 # param_5 = obj.isEmpty() 58 # param_6 = obj.isFull()
1 class Node: 2 __slots__ = 'prev', 'next', 'val' 3 4 def __init__(self, val): 5 self.prev = self.next =None 6 self.val = val 7 class MyCircularDeque: #利用循环队列实现双端队列,利用双向链表模拟双端队列 8 #构造函数,双端队列最大为 k 9 def __init__(self, k: int): 10 #也初始化头尾指针 11 self.head = self.tail = None 12 self.capacity = k 13 self.size = 0 14 15 def insertFront(self, value: int) -> bool: 16 #将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 17 if self.isFull(): 18 return False 19 node = Node(value) 20 #空列表 21 if self.isEmpty(): 22 self.head = node 23 self.tail = node 24 else: 25 node.next = self.head #尾 26 self.head.prev = node #头 27 self.head = node 28 self.size += 1 29 30 return True 31 32 def insertLast(self, value: int) -> bool: 33 #将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 34 if self.isFull(): 35 return False 36 node = Node(value) 37 if self.isEmpty(): 38 self.head = node 39 self.tail = node 40 else: 41 self.tail.next = node 42 node.prev = self.tail 43 self.tail = node 44 self.size += 1 45 46 return True 47 48 def deleteFront(self) -> bool: 49 #从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 50 if self.isEmpty(): 51 return False 52 self.head = self.head.next 53 #别忘了head的prev指向None 54 if self.head: 55 self.head.prev = None 56 self.size -= 1 57 58 return True 59 60 def deleteLast(self) -> bool: 61 #从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 62 if self.isEmpty(): 63 return False 64 self.tail = self.tail.prev 65 #别忘了tail的next指向None 66 if self.tail: 67 self.tail.next = None 68 self.size -= 1 69 70 return True 71 def getFront(self) -> int: 72 #从双端队列头部获得一个元素。如果双端队列为空,返回 -1 73 return -1 if self.isEmpty() else self.head.val 74 def getRear(self) -> int: 75 #获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 76 return -1 if self.isEmpty() else self.tail.val 77 78 def isEmpty(self) -> bool: 79 #若双端队列为空,则返回 true ,否则返回 false 80 return self.size == 0 81 82 def isFull(self) -> bool: 83 #若双端队列满了,则返回 true ,否则返回 false 84 return self.size == self.capacity 85 86 87 # Your MyCircularDeque object will be instantiated and called as such: 88 # obj = MyCircularDeque(k) 89 # param_1 = obj.insertFront(value) 90 # param_2 = obj.insertLast(value) 91 # param_3 = obj.deleteFront() 92 # param_4 = obj.deleteLast() 93 # param_5 = obj.getFront() 94 # param_6 = obj.getRear() 95 # param_7 = obj.isEmpty() 96 # param_8 = obj.isFull()