[leetcode] Min Stack @ Python

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

解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列。

代码:

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[len(self.stack2)-1]:
            self.stack2.append(x)

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

    # @return an integer
    def top(self):
        return self.stack1[len(self.stack1)-1]

    # @return an integer
    def getMin(self):
        return self.stack2[len(self.stack2)-1]
时间: 2024-07-30 21:23:16

[leetcode] Min Stack @ Python的相关文章

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top

[LeetCode] Min Stack Min Stack

Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the

【leetcode】Min Stack -- python版

题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.getMin() -- Retrieve the minimum

LeetCode——Min Stack

Description: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve

LeetCode Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

[LeetCode] Min Stack 栈

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

LeetCode() Min Stack 不知道哪里不对,留待。

class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1) coll.resize(2*coll.size()); coll[++index]=x; if(x<min) { min=x; flag=index; } } void pop() { index--; } int top() { return coll[index]; } int getMin

[LeetCode]Min Stack,解题报告

目录 目录 题目 思路1 AC代码 缺陷 思路2 AC代码 题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getM

LeetCode[stack]: Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. - push(x) – Push element x onto stack. - pop() – Removes the element on top of the stack. - top() – Get the top element. - getMin() – Retrieve the minim