LintCode Min Stack
用两个stack, 第一个按顺序放所有值,第二个只放当前最小值。
注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个; 2. pop时若两个stack的最上面值相等则都pop, 不等则只pop第一个stack, 但是都得返回第一个stack的pop值; 3. min时只返回第二个stack的peek值。
public class MinStack {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public MinStack() {
// do initialize if necessary
}
public void push(int number) {
stack1.push(number);
if(stack2.empty()){
stack2.push(number);
} else{
if(stack2.peek() >= number){
stack2.push(number);
}
}
// write your code here
}
public int pop() {
if(stack1.empty() || stack2.empty()){
return -1;
}
if(stack1.peek().equals(stack2.peek())){
stack2.pop();
return stack1.pop();
} else{
return stack1.pop();
}
// write your code here
}
public int min() {
return stack2.peek();// write your code here
}
}
LintCode Min Stack的更多相关文章
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- Android studio打开之后 cannot load project: java.lang.NUllpointerException
参考来源:http://bbs.csdn.net/topics/391014393 关闭网络,重新打开Android studio就好了.(但是原因不清楚是为什么?) Internal error. ...
- kudu playground
建表: CREATE TABLE my_first_table ( id BIGINT, name STRING ) TBLPROPERTIES( 'storage_handler' = 'com.c ...
- Unhandled exception at 0x........ in XXXX.exe: 0xC0000005:错误
对于C++初学者或粗心者,很容易犯如下图所示错误: 那么该错误是由什么造成的呢? 答案无疑只有一个,即:读取了本该没有的值或者地址. 那么如何解决呢? 第一件事,检查下你传入的参数 ...
- svn图标不显示的解决方案
最近发现svn图标莫名其妙的不显示,其他操作都正常.在网上搜了一些方法. 解决方法一(失败): 升级最新版本,我的本来就是最新版本 解决方法二(失败): 右键->TortoiseSVN-> ...
- css中的1px并不总等于设备的1px(高分辨率不等 低分辨等)
在css中我们一般使用px作为单位,在桌面浏览器中css的1个像素往往都是对应着电脑屏幕的1个物理像素,这可能会造成我们的一个错觉,那就是css中的像素就是设备的物理像素.但实际情况却并非如此,css ...
- tomcat+nginx简单实现负载均衡
1.环境准备 在前面的博客中我已经安装好nginx和一台tomcat了.现在就在加一台tomcat tomcat1: /apps/tomcat/tomcat1/apache-tomcat-7.0.6 ...
- cookie 保存上次访问url方法
if (Session[Enums.UserInfoSeesion] == null) { HttpCookie cookie = Request.Cookies[Enums.UserLastAcce ...
- MVC 请求处理流程(二)
[上一篇]中我们说到了对象AsyncControllerActionInvoker,在Controller的ExecuteCore方法中调用AsyncControllerActionInvoker对象 ...
- centos7下安装nodejs
http://www.runoob.com/nodejs/nodejs-install-setup.html这个教程中注明 注意:Linux上安装Node.js需要安装Python 2.6 或 2.7 ...
- 用户新加入Group
Linux中一个user可以加入多个分组 以下以用户holmes为例,原始用户组为holmes,新加入用户组users 首先进入root模式 [holmes@KirCentOS share]$ su ...