leetcode 155. Min Stack --------- java
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.
设计一个可以获得最小值的栈。
第一次用了ArrayList和Stack,超时了。
public class MinStack { Stack<Integer> stack ;
ArrayList<Integer> list;
public MinStack() {
stack = new Stack<Integer>();
list = new ArrayList<Integer>();
} public void push(int x) {
stack.push(x);
int flag = 0;
if( list.size() == 0 ){
list.add(x);
return ;
}
for( int i = 0 ; i < list.size() && flag == 0;i++){
if( list.get(i) > x ){
list.add(i,x);
flag = 1;
}
}
if( flag == 0)
list.add(list.size(),x); } public void pop() { list.remove((Integer) stack.pop());
} public int top() {
return stack.peek();
} public int getMin() {
return list.get(0);
}
} /**
* 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();
*/
只使用一个ArrayList
public class MinStack { ArrayList<Integer> list;
int min ;
public MinStack() {
list = new ArrayList<Integer>();
} public void push(int x) {
list.add(x);
if( list.size() == 1)
min = x;
else
min = Math.min(min,x);
} public void pop() {
list.remove( list.size()-1 );
if( list.size() == 0)
return ;
min = list.get(0);
for( int i = 1 ; i < list.size();i++)
min = Math.min(min,list.get(i)); } public int top() {
return list.get(list.size()-1);
} public int getMin() {
return min;
}
} /**
* 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();
*/
3、使用两个栈实现。
public class MinStack { private Stack<Integer> stack = new Stack<Integer>();
// minStack: store the current min values
private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) {
// store current min value into minStack
if (minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
} public void pop() {
// use equals to compare the value of two object, if equal, pop both of them
if (stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
} /**
* 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();
*/
4、也可以只用一个栈实现。
public class MinStack {
long min;
Stack<Long> stack; public MinStack(){
stack=new Stack<>();
} public void push(int x) {
if (stack.isEmpty()){
stack.push(0L);
min=x;
}else{
stack.push(x-min);//Could be negative if min value needs to change
if (x<min) min=x;
}
} public void pop() {
if (stack.isEmpty()) return; long pop=stack.pop(); if (pop<0) min=min-pop;//If negative, increase the min value } public int top() {
long top=stack.peek();
if (top>0){
return (int)(top+min);
}else{
return (int)(min);
}
} public int getMin() {
return (int)min;
}
}
5、不用栈实现。
class MinStack {
private Node head; public void push(int x) {
if(head == null)
head = new Node(x, x);
else
head = new Node(x, Math.min(x, head.min), head);
} public void pop() {
head = head.next;
} public int top() {
return head.val;
} public int getMin() {
return head.min;
} private class Node {
int val;
int min;
Node next; private Node(int val, int min) {
this(val, min, null);
} private Node(int val, int min, Node next) {
this.val = val;
this.min = min;
this.next = next;
}
}
}
leetcode 155. Min Stack --------- java的更多相关文章
- 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 ...
- 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(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...
- Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
随机推荐
- Oracle ODP.NET 篇
1.C# 使用 System.Data.OracleClient 连接 Oracle 需要安装 instantclient , 并配置相应环境变量.重启,方可使用. 2. 使用 System.Data ...
- java中的日志组件-log4j
1.为什么使用日志组件 Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT的事件记录器:还可以控制每一 ...
- Google search
filetype: active directory filetype: eg : lady gaga filetype:mp3 link: eg : link : supinfo.com(链接到su ...
- SQL Server 语句整理
1. 创建数据库 create database dbName 2. 删除数据库 drop database dbName 3. 备份sql server --- 创建 备份数据的 device US ...
- poj1647
转自:http://woodjohn.blog.sohu.com/231905679.html 题意是比较简单的:假定你是国际象棋中的白方,现在棋盘上只剩下白王.黑王和白后(王和后的走法规则就不赘述了 ...
- Eclipse中一些快捷键
用到哪里更新到哪里~~~ 1,代码自动对齐 ctrl+shift+f 2,自动填充相关的包 alt+/ 注意选择好需要的包 3,注释某几行代码 选定后ctrl+/ 4,设置自动补全 最简单的修改方 ...
- Ogre中TerrainSceneManager
转自:http://blog.csdn.net/yanonsoftware/article/details/1103665 TerrainSceneManager是一个OctreeSceneManag ...
- C++11 std::copy
这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...
- linux添加动态库搜索路径
在有时运行程序出现动态库找不着的问题,而明明装了的.这时候可能是没有将相应的路径添加到系统中去. 具体说:cd /etc/ld.so.conf.d/ 可以发现里面有一堆*.conf的文件 我们要做的就 ...
- Tips about Object-oriented programming
1, Return subinterface For example, we have a parent interface: public interface A<T extends A< ...