LeetCode155 栈·最小栈(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.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
/*解题思路:
在入栈时,如果这个元素比最小值小,那么,我们让最小值入栈,然后将元素值赋给最小值,即新的最小值,然后正常将这个元素入栈;在出栈时,如果栈顶元素与最小值相等,说明他的下一个元素是之前push的最小值(上一个),出栈后,将这个之前的最小值赋值给最新的最小值。(每次push进去的最小值都是下面元素的最小值)
*/
class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {}
    void push(int x) {
        if (minstack.empty())
        {
            minstack.push(x);
            min = x;
        }
        else {
            if (x<=min)//此处必须为<=,否则可以试一下{0,1,0}的情况
            {
                minstack.push(min);
                min = x;
            }
            minstack.push(x);
        }
    }

    void pop() {
        if (minstack.empty())
            return;
        int index = this->top();
        minstack.pop();
        if (min == index && !minstack.empty())
        {
            min = this->top();
            minstack.pop();
        }
    }

    int top() {
        return minstack.top();
    }

    int getMin() {
        return min;
    }
private:
    int min;
    stack<int>minstack;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

原文地址:https://www.cnblogs.com/parzulpan/p/9924571.html

时间: 2024-08-29 01:09:40

LeetCode155 栈·最小栈(C++)的相关文章

栈————最小栈

1 class MinStack { 2 public: 3 /** initialize your data structure here. */ 4 MinStack() { 5 6 } 7 stack<int> s; 8 void push(int x) { 9 if(s.empty() || x<=s.top()){ 10 s.push(x); 11 s.push(x); 12 } 13 else{ 14 int tmp=s.top(); 15 s.push(x); 16 s.p

最小栈

思想分析: 实现方案: 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个元素的最小值,这样两个栈的长度是始终一样的 入栈:最小栈需要加入的元素是 当前要入的元

实现栈最小元素的min函数

1 #include<iostream> 2 #include<stack> 3 using namespace std; 4 class min_stack 5 { 6 public: 7 void push(int); 8 void pop(); 9 int min(); 10 int size() 11 { 12 return data.size(); 13 } 14 private: 15 stack<int> data; 16 stack<int>

[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() –

最小栈的实现和优化

https://mp.weixin.qq.com/s/q5wtEXg_tC-wlyK1uMlJJA 最小栈 实现一个最小栈,一步一步优化,空间O(N) 时间O(1) . import java.util.ArrayList; import java.util.List; /** * @author xiaoshi on 2018/9/1. */ public class MinStack { private List<Integer> data = new ArrayList<Integ