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 ...
分类:
其他好文 时间:
2014-12-04 12:21:07
阅读次数:
128
3.2Howwouldyoudesignastackwhich,inadditiontopushandpop,alsohasafunctionminwhichreturnstheminimumelement?Push,popandminshouldalloperateinO(1)time.Useanotherstackmaintainingmin.
push(Tt)
{
mainStack.push(t);
TcurMin=minStack.peek();
if(curMin==null||t<curM..
分类:
其他好文 时间:
2014-11-24 12:08:38
阅读次数:
183
原题地址:https://oj.leetcode.com/problems/min-stack/解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列。代码:class MinStack: # @param x, an integer def __init__(self): ...
分类:
编程语言 时间:
2014-11-12 13:44:19
阅读次数:
213
一共用两个栈。一个用来放数据,另一个专门用来存放当前最小值。 1 class MinStack { 2 public: 3 void push(int x) { 4 elements.push(x); 5 if (mins.empty()||x element...
分类:
其他好文 时间:
2014-11-10 19:45:57
阅读次数:
143
就是用另外一个单调stack来记录最小值就可以了,这个队列单调递减。class MinStack {public: void push(int x) { st.push(x); if (stm.empty() || stm.top() >= x) stm.push(...
分类:
其他好文 时间:
2014-11-10 11:16:35
阅读次数:
157