标签:int ram ilo splay init anti cdb new pen
MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // returns 1 queue.pop(); // returns 1 queue.empty(); // returns false
class MyQueue: def __init__(self): self.s1 = [] self.s2 = [] def push(self, x: int) -> None: while self.s1: self.s2.append(self.s1.pop()) self.s1.append(x) while self.s2: self.s1.append(self.s2.pop()) def pop(self) -> int: return self.s1.pop() def peek(self) -> int: return self.s1[-1] def empty(self) -> bool: return not self.s1 # Your MyQueue object will be instantiated and called as such: # obj = MyQueue() # obj.push(x) # param_2 = obj.pop() # param_3 = obj.peek() # param_4 = obj.empty()
class MyQueue: def __init__(self): self.s1 = [] self.s2 = [] def push(self, x: int) -> None: self.s1.append(x) def pop(self) -> int: self.peek() return self.s2.pop() def peek(self) -> int: if not self.s2: while self.s1: self.s2.append(self.s1.pop()) return self.s2[-1] def empty(self) -> bool: return not self.s1
MyStack stack = new MyStack(); stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // returns false
class Stack: def __init__(self): self._queue = collections.deque() def push(self, x): q = self._queue q.append(x) for _ in range(len(q) - 1): q.append(q.popleft()) def pop(self): return self._queue.popleft() def top(self): return self._queue[0] def empty(self): return not len(self._queue)
标签:int ram ilo splay init anti cdb new pen
原文地址:https://www.cnblogs.com/ybxw/p/12763309.html