本文共 867 字,大约阅读时间需要 2 分钟。
class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.s1=[] self.s2=[] def push(self, x): """ :type x: int :rtype: void """ self.s1.append(x) if self.s2: if x > self.s2[-1]: self.s2.append(self.s2[-1]) else: self.s2.append(x) else: self.s2.append(x) def pop(self): """ :rtype: void """ self.s1.pop() self.s2.pop() def top(self): """ :rtype: int """ return self.s1[-1] def getMin(self): """ :rtype: int """ return self.s2[-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()
转载地址:http://rgxhj.baihongyu.com/