[leetcode] Min Stack @ Python

原題地址:https://oj.leetcode.com/problems/min-stack/app

解題思路:開闢兩個棧,一個棧是普通的棧,一個棧用來維護最小值的隊列。spa

代碼:code

class MinStack:
    # @param x, an integer
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    # @return an integer
    def push(self, x):
        self.stack1.append(x)
        if len(self.stack2) == 0 or x <= self.stack2[-1]:
            self.stack2.append(x)

    # @return nothing
    def pop(self):
        top = self.stack1[-1]
        self.stack1.pop()
        if top == self.stack2[-1]:
            self.stack2.pop()
        
    # @return an integer
    def top(self):
        return self.stack1[-1]

    # @return an integer
    def getMin(self):
        return self.stack2[-1]
相關文章
相關標籤/搜索