LeetCode(64)- 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 element in the stack.
思路:
- 题意:给出四个函数API,构造一个stack,而且能够返回最小值
- 用双栈的策略,一个用来正常的存储,一个用来存贮最小值
- 注意比较的时候。peek()函数要用equals函数比较,因为弹出的是对象
代码:
class MinStack {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> min = new Stack<Integer>();
public void push(int x) {
if(min.isEmpty() || x <= min.peek()){
min.push(x);
}
stack.push(x);
}
public void pop() {
if(stack == null){
return;
}
if(min.peek().equals(stack.peek())){
min.pop();
}
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min.peek();
}
}
LeetCode(64)- Min Stack的更多相关文章
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 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 ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- Linux--FTP和MAIL服务器
1) FTP协议 FTP(FileTransfer Protocol,文件传输协议)用于管理计算机之间的文件传送.FTP 是Internet 上使用非常广泛的一种通讯协议,它是由支持Intern ...
- 剑指offer面试题3 二维数组中的查找(c)
剑指offer面试题三:
- 在O(1)时间内删除单链表结点
给定单链表的一个结点的指针,同时该结点不是尾结点,此外没有指向其它任何结点的指针,请在O(1)时间内删除该结点. int deleteNode(LNode **head, LNode **node) ...
- TCP连接建立系列 — 客户端发送SYN段
主要内容:客户端调用connect()时的TCP层实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd connect的TCP层实现 SOCK_STRE ...
- 【移动开发】Context类bindService()参数
bindService()是Context的一个方法,它是抽象的.函数原型的代码如下:(android 2.3.3) /** * Connect to an application service, ...
- MySQL输入密码后闪退,这里有解
不知道怎么的,我的MySQL就像抽风了一样,可能是不想理我了吧.只要我输入密码,它就会闪退.而且使用其他的数据库管理工具也是链接不成功的.于是下决心,调教调教它,于是有了下面的这些解决方案. 解决方法 ...
- Android开发学习之路--异步消息Handler,Message,Looper和AsyncTask之初体验
在简易音乐播放器中,用了Handler,也没有过多地去研究学习,这里再学习下android下的异步消息处理机制.这里用了Handler主要是在线程中不能更新UI,而需要通过Handler才可以.关于异 ...
- hashmap 循环取出所有值 取出特定的值 两种方法
//第一种 Iterator menus = menu.iterator(); while(menus.hasNext()) { Map userMap = (Map) menus.next(); S ...
- ubuntu文件管理常用命令
1.关闭防火墙:ufw disable 2.以.开头的表示隐藏文件 3..和..分别代表当前目录以及当前目录的父目录 4.显示当前用户所在目录pwd 5.touch创建空文件 6.mkdir创建新目录 ...
- 03 ProgressBar 进度条
> style="?android:attr/progressBarStyleSmall" 样式 android:progress= ...