当前位置: 移动技术网 > IT编程>开发语言>C/C++ > LeetCode()- Min Stack

LeetCode()- Min Stack

2018年03月31日  | 移动技术网IT编程  | 我要评论

visit的现在分词,一代屠神林世荣,李璐珂

题目:

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.

思路:

题意:给出四个函数API,构造一个stack,而且能够返回最小值 用双栈的策略,一个用来正常的存储,一个用来存贮最小值 注意比较的时候。peek()函数要用equals函数比较,因为弹出的是对象

代码:

class MinStack {
    Stack stack = new Stack();
    Stack min = new Stack();
    public void push(int x) {
        if(min.isEmpty() || x 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网