def__init__(self): """ Initialize your data structure here. """ self.myList = []
defpush(self, x: int) -> None: """ Push element x to the back of queue. """ self.myList.append(x)
defpop(self) -> int: """ Removes the element from in front of queue and returns that element. """ return self.myList.pop(0)
defpeek(self) -> int: """ Get the front element. """ return self.myList[0]
defempty(self) -> bool: """ Returns whether the queue is empty. """ return len(self.myList) == 0
# 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()
def __init__(self): """ Initialize your data structure here. """ self.myList = []
def push(self, x: int) -> None: """ Push element x to the back of queue. """ self.myList.append(x) def pop(self) -> int: """ Removes the element from in front of queue and returns that element. """ return self.myList.pop(0) def peek(self) -> int: """ Get the front element. """ return self.myList[0] def empty(self) -> bool: """ Returns whether the queue is empty. """ return len(self.myList)== 0
# 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()
def__init__(self): """ Initialize your data structure here. """ self.myList = []
defpush(self, x: int) -> None: """ Push element x onto stack. """ self.myList.append(x)
defpop(self) -> int: """ Removes the element on top of the stack and returns that element. """ return self.myList.pop()
deftop(self) -> int: """ Get the top element. """ return self.myList[-1]
defempty(self) -> bool: """ Returns whether the stack is empty. """ return len(self.myList) == 0
# Your MyStack object will be instantiated and called as such: # obj = MyStack() # obj.push(x) # param_2 = obj.pop() # param_3 = obj.top() # param_4 = obj.empty()
def__init__(self): """ initialize your data structure here. """ self.myList = [] self.minList = []
defpush(self, x: int) -> None: self.myList.append(x) ifnot self.minList or x <= self.minList[-1]: self.minList.append(x)
defpop(self) -> None: if self.myList[-1] == self.minList[-1]: self.minList.pop() self.myList.pop()
deftop(self) -> int: return self.myList[-1]
defgetMin(self) -> int: return self.minList[-1]
# Your MinStack object will be instantiated and called as such: # obj = MinStack() # obj.push(x) # obj.pop() # param_3 = obj.top() # param_4 = obj.getMin()
classSolution: defnextGreaterElements(self, nums: List[int]) -> List[int]: nums2 = nums * 2 stack = [] rlt = [-1] * len(nums2) for i in range(len(nums2)): while len(stack) != 0and nums2[i] > nums2[stack[-1]]: preIndex = stack.pop() rlt[preIndex] = nums2[i] stack.append(i) return rlt[:len(nums)]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicint[] nextGreaterElements(int[] nums) { int n = nums.length; int[] next = newint[n]; Arrays.fill(next, -1); Stack<Integer> pre = new Stack<>(); for (int i = 0; i < n * 2; i++) { int num = nums[i % n]; while (!pre.isEmpty() && nums[pre.peek()] < num) { next[pre.pop()] = num; } if (i < n){ pre.push(i); } } return next; }