LeetCode()- Min Stack
程序员文章站
2022-04-19 15:55:26
题目:
Design a stack that supports push, pop, top, and retrieving the minimum element in...
题目:
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
上一篇: C++牌型种数题解
推荐阅读
-
【leetcode】存在重复【C、Python】
-
【Leetcode】724-寻找数组的中心索引(Find Pivot Index)
-
html5shiv.js和respond.min.js
-
荐 Leetcode_栈与队列
-
5、有效的括号-Python-LeetCode-20
-
Elastic Stack 开源的大数据解决方案
-
Stack Overflow上59万浏览量的提问:为什么会发生ArrayIndexOutOfBoundsException?
-
leetcode - 括号字符串是否有效
-
JQuery中关于jquery.js与jquery.min.js的比较探讨
-
详解Numpy中的数组拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)