设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈。 push(x) -- 将元素x推入栈中。 pop() -- 删除栈顶的元素。 top() -- 获取栈顶元素。 getMin() -- 检索栈中的最小元素。示例:MinStack minStack = new MinS ...
分类:
其他好文 时间:
2018-04-06 18:33:15
阅读次数:
180
[抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。 [思维问题]: [一句话思路]: 用一个minstack来辅助实现 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊 ...
分类:
其他好文 时间:
2018-01-08 13:40:14
阅读次数:
130
一、题目 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。 二、思路 用一个栈dataStack保存数据,用另外一个栈minStack保存依次入栈最小的数。每次元素存入minStack的时候,如果该元素比minStack的栈顶元素小,则存入minStack,否则用minStac ...
分类:
其他好文 时间:
2017-10-02 21:32:38
阅读次数:
182
1 Min Stack public class MinStack { private Stack<Integer> stack = new Stack(); private Stack<Integer> min = new Stack(); public MinStack() { // do in ...
分类:
其他好文 时间:
2017-08-28 23:42:35
阅读次数:
199
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。 /* * 1.dataStack为存储数据的栈,minStack为存储最小值的栈; * 2.push的时候将value值与minStack中的to ...
分类:
其他好文 时间:
2017-08-07 15:20:08
阅读次数:
106
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Remove ...
分类:
其他好文 时间:
2017-04-26 14:45:05
阅读次数:
159
Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) c ...
分类:
其他好文 时间:
2016-08-25 23:48:23
阅读次数:
163
思想分析: 实现方案: class MinStack { public: MinStack() {} void push(int x) { if (StackNum.empty()) { StackNum.push(x); StackMin.push(x); } else { if (x <= St ...
分类:
其他好文 时间:
2016-07-12 23:25:28
阅读次数:
276
这里面有一个注意的点是,如果现在要push x,且x等于当前存在的最小值(栈中已经有了x)时,也要把x进栈。否则当x出栈时,当前最小值也跟着出栈,但是原栈中最小值元素依然还有,此时最小值却发生了错误。 class MinStack { public: /** initialize your data ...
分类:
其他好文 时间:
2016-05-23 00:29:25
阅读次数:
167
题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin。 题目思路: 私用是用两个数组来处理。 代码(python): 1 class MinStack(object): 2 de ...
分类:
编程语言 时间:
2016-05-10 18:19:32
阅读次数:
214