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.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
 1 class MinStack {
 2 public:
 3     /** initialize your data structure here. */
 4     std::stack<int> stack1,stack2;
 5     MinStack() {
 6
 7     }
 8
 9     void push(int x) {
10         stack1.push(x);
11         if(stack2.empty())
12             stack2.push(x);
13         else if( x  <= stack2.top())
14             stack2.push(x);
15     }
16
17     void pop() {
18         int num1;
19         // write your code here
20         if(stack1.top()==stack2.top())
21         {
22             stack1.pop();
23             num1 =  stack2.top();
24             stack2.pop();
25         }else {
26            num1 = stack1.top();
27            stack1.pop();
28         }
29
30     }
31
32     int top() {
33         int num = stack1.top();
34         return  num;
35     }
36
37     int getMin() {
38         int num = stack2.top();
39         return  num;
40     }
41 };
42
43 /**
44  * Your MinStack object will be instantiated and called as such:
45  * MinStack obj = new MinStack();
46  * obj.push(x);
47  * obj.pop();
48  * int param_3 = obj.top();
49  * int param_4 = obj.getMin();
50  */

原文地址:https://www.cnblogs.com/jj81/p/10068277.html

时间: 2024-07-31 15:22:29

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

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

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

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): &qu

最小栈

思想分析: 实现方案: 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

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

【算法】最小栈

实现一个栈,带有出栈(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个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元

[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 最小栈.

【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】

[155-Min Stack(最小栈)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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() –