一天一道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. Delphi Inputbox,InputQuery用法

    Delphi :InputQuery,InputBox用法及区别 function InputQuery(const ACaption, APrompt: string; var Value: str ...

  2. C语言程序设计第三次作业——选择结构(一)

    (一)改错题 错误信息: 错误原因:y=1/x后没加分号 改正方法:在其后加上分号 错误信息: 错误原因:if语句后接了:,使else语句找不到对应的if 改正方法:删掉if后的分号 错误信息: 错误 ...

  3. strings.h 与 string.h 头文件的区别

    今天使用 man string 来查看 string 文件的使用的方法(毕竟里面的函数名字和传入参数和发挥参数的类型,如果一段时间不使用,会产生遗忘.) 偶然发现,string.h 的man page ...

  4. python2.7入门---内置函数

        内置函数     abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() ...

  5. mybatis源码解读(一)——初始化环境

    本系列博客将对mybatis的源码进行解读,关于mybatis的使用教程,可以查看我前面写的博客——传送门. 为了便于后面的讲解,我们这里首先构造一个统一环境.也可以参考mybatis官网. 1.数据 ...

  6. sqlserver 按照特定值排序查询结果

    select * from t_ss_student order by case when xm like '林%' then 1 else 2 end asc; 姓林的会排在前面

  7. 取list的值

    list.get(0):之类的我就不写了 我就写一个我老忘记的 Iterator it = list.iterator(); while(it.hasNext()){ Student stu = it ...

  8. BlockingQueue阻塞队列(解决多线程中数据安全问题 可用于抢票,秒杀)

    案例:一个线程类中 private static BlockingQueue<Map<String, String>> dataQueue = new LinkedBlocki ...

  9. 152. Maximum Product Subarray(中等, 神奇的 swap)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. 牛客网编程练习之PAT乙级(Basic Level):1034 写出这个数

    AC代码: import java.util.*; /** * @author CC11001100 */ public class Main { public static void main(St ...