一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

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.

(二)解题

题目大意:实现一个最小栈,在常量时间内完成push,pop,top和getmin等操作

解题思路:用两个栈,一个栈存储所有的数字,另一个栈存储最小数。

具体思路见代码:

class MinStack {
public:
    /** initialize your data structure here. */
    stack<int> datastack;//存储数据
    stack<int> minstack;//存储最小栈
    MinStack() {

    }
    void push(int x) {
        datastack.push(x);//压入数据
        if(minstack.empty()) minstack.push(x);//如果最小栈为空直接压入
        else if(x<=minstack.top()) minstack.push(x);//如果当前压入的值小于等于最小栈的栈顶元素,则压入最小栈
    }

    void pop() {
        if(datastack.top()==minstack.top()) minstack.pop();//如果数据栈和最小栈的栈顶元素相等,则最小栈栈顶元素弹出
        datastack.pop();//数据栈弹出元素

    }

    int top() {
        return datastack.top();//返回栈顶元素
    }

    int getMin() {
        return minstack.top();//返回最小栈栈顶元素
    }
};
/**
 * 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();
 */

【一天一道LeetCode】#155. Min Stack的更多相关文章

  1. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  2. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  4. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. Java for LeetCode 155 Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  7. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  8. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

  9. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

随机推荐

  1. 用Qemu运行/调试arm linux【转】

    转自:https://blog.csdn.net/absurd/article/details/78984244 用Qemu运行/调试arm linux,这事情干过好几次了,久了就忘记了,每次都要重新 ...

  2. xml 制作 RSS 订阅源

    首先制作一个 RSS 模板,模板的文件名是 feed.xml,代码如下: <?xml version="1.0" encoding="utf-8"?> ...

  3. UDA机器学习基础—异常值-安然数据处理

    #!/usr/bin/python import pickle import sys import matplotlib.pyplot sys.path.append("../tools/& ...

  4. WPF 实现换肤功能

    将所有控件的基本样式汇集到一个资源字典中,构成界面的基本样式文件,然后进行不同颜色皮肤的定制. 即在新的皮肤资源字典文件中引入基本样式文件,然后使用资源继承,并且只设置控件的颜色属性等,形成一个皮肤文 ...

  5. TypeScript: Week Reflection

    TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...

  6. webpack require.ensure 按需加载

    使用 vue-cli构建的项目,在 默认情况下 ,会将所有的js代码打包为一个整体比如index.js.当使用存在多个路由代码的时候,index.js可能会超大,影响加载速度. 这个每个路由页面除了i ...

  7. linux的简单命令 网络配置

    1.1.1 ls命令 l ls(list)功能:列出目录内容 l 格式:ls [参数] [文件或目录] -a或--all   下所有文件和目录.注意隐藏文件.特殊目录.. 和 .. -l   使用详细 ...

  8. Spring消息之AMQP.

    一.AMQP 概述 AMQP(Advanced Message Queuing Protocol),高级消息队列协议. 简单回忆一下JMS的消息模型,可能会有助于理解AMQP的消息模型.在JMS中,有 ...

  9. SQL_CALC_FOUND_ROWS equivalent in PostgreSQL

    https://www.postgresql.org/message-id/1185863074.10580.91.camel%40linda.lfix.co.uk On Tue, 2007-07-3 ...

  10. 豌豆夹Redis解决方案Codis源码剖析:Dashboard

    豌豆夹Redis解决方案Codis源码剖析:Dashboard 1.不只是Dashboard 虽然名字叫Dashboard,但它在Codis中的作用却不可小觑.它不仅仅是Dashboard管理页面,更 ...