leetcode——155. 最小栈

class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self._elem=[]

    def push(self, x):
        """
        :type x: int
        :rtype: None
        """
        self._elem.append(x)

    def pop(self):
        """
        :rtype: None
        """
        return self._elem.pop()

    def top(self):
        """
        :rtype: int
        """
        return self._elem[-1]

    def getMin(self):
        """
        :rtype: int
        """
        return min(self._elem)

# 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()

执行用时 :660 ms, 在所有 python 提交中击败了24.93%的用户

内存消耗 :15.5 MB, 在所有 python 提交中击败了19.17%的用户

——2019.11.2

原文地址:https://www.cnblogs.com/taoyuxin/p/11782407.html

时间: 2024-08-30 17:08:08

leetcode——155. 最小栈的相关文章

leetcode 155. 最小栈(c++)

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶元素.getMin() -- 检索栈中的最小元素.示例: MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin(); --> 返回 -3.minStack.p

155. 最小栈

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. getMin() -- 检索栈中的最小元素. 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. m

leadcode的Hot100系列--155. 最小栈

栈:先入后出,后入先出 像电梯一样,先进入电梯的,走到电梯最深处,后进入电梯的,站在电梯门口, 所以电梯打开的时候,后进入的会先走出来,先进入的会后走出来. push,对应入电梯,把数据往里面压 pop, 对应出电梯,把数据往外拿 栈顶,对应电梯门口 栈底,对应电梯最深处 这里使用链表实现栈. 先创建一个MinStack头, 入栈:直接把结构体挂在MinStack头后面, 出栈:直接拿出MinStack头后面的结构体. 取最小值:对链表进行一次遍历,返回最小值. typedef struct m

LeetCode OJ: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 155

155. 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() -- Retriev

[CareerCup] 3.2 Min Stack 最小栈

3.2 How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.

最小栈

思想分析: 实现方案: class MinStack { public: MinStack() {} void push(int x) { if (StackNum.empty()) { StackNum.push(x); StackMin.push(x); } else { if (x <= StackMin.top()) StackMin.push(x); StackNum.push(x); } } void pop() { if (StackMin.top() == StackNum.to

【算法】最小栈

实现一个栈,带有出栈(pop).入栈(push).取最小元素(getMin)三个方法,且时间复杂度均为O(1). 初始想法: 创建 int min = -1,记录栈中最小元素的下标: 第一个元素进栈时,min = 0: 每当新元素进栈,让新元素与 min 下标位置的元素比较大小,min = 较小元素的下标: 调用 getMin 直接返回 min 的值. 这种方式进栈没有问题,而出栈时,若当前最小元素在栈顶并出栈了,用剩下哪个元素的下标顶替当前 min 就不得而知了.所以一旦最小元素的下标出栈,需

lintcode 中等题:Min stack 最小栈

题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义一个数组或者其他的存储最小值,第i个元素,表示栈中前i个元素的最小值. 定义两个ArrayList来存储栈,一个ArrayList存储当前栈中的元素,一个ArrayList存储最小栈,并且其第i个元素表示栈中前i个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元