美文网首页
155. Min Stack

155. Min Stack

作者: 高思阳 | 来源:发表于2018-10-18 13:43 被阅读6次

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 element in the stack.
    

译:设计一个支持push / pop / top操作的栈,能够在常量时间取得最小值
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.

问题分析
题中为了取当前栈mStack中最小值,一开始想的是用常量来做一个缓存,但发现最小值是根据栈mStack的内容动态变化的,这就意味着需要一个额外的栈minValueStack进行缓存。别的就是正常的栈操作,注意边界和空栈的情况即可。不过发现一个问题,如果原始栈一直处于递减状态,那么栈minValueStack会一直递增花费更多的空间,而一旦题目对空间的要求比较苛刻,这种方式就不再适用。

相关文章

  • leetcode:155. Min Stack

    155. Min Stack Description Design a stack that supports p...

  • Leetcode-155Min Stack

    155. Min Stack && 剑指offer-包含min函数的栈 Design a stack that s...

  • 155. Min Stack

    Design a stack that supports push, pop, top, and retrievi...

  • 155. Min Stack

    Problem Design a stack that supports push, pop, top, and ...

  • 155. Min Stack

    Description: Design a stack that supports push, pop, top,...

  • 155. Min Stack

    1.描述 Design a stack that supports push, pop, top, and ret...

  • 155. Min Stack

    题目 Design a stack that supports push, pop, top, and retri...

  • 155. Min Stack

    Description: Design a stack that supports push, pop, top,...

  • 155. Min Stack

    Design a stack that supports push, pop, top, and retrievi...

  • 155. Min Stack

    Design a stack that supports push, pop, top, and retrievi...

网友评论

      本文标题:155. Min Stack

      本文链接:https://www.haomeiwen.com/subject/jynjzftx.html