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的更多相关文章

  1. 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 ...

  2. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  3. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  4. Java for LeetCode 155 Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0

    https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...

  7. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

  8. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  9. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

随机推荐

  1. gsoap框架下的onvif程序流程分析

    SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap) { do { unsigned int k = soap->max_keep_al ...

  2. objective-c strong导致内存泄漏简单案例

    例如: @interface Test:NSObject{ id __strong obj_; } -(void) setObject:(id __strong)obj; @end @implemen ...

  3. 一篇介绍jquery很好的

    本文基于jQuery1.7.1版本,是对官方API的整理和总结,完整的官方API见http://api.jquery.com/browser/ 0.总述 jQuery框架提供了很多方法,但大致上可以分 ...

  4. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  5. C#获取项目程序及运行路径的方法

    1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”: 2.c# winform用 A:“Applicat ...

  6. org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors

    最近在项目中发现如下异常: 六月 25, 2015 5:58:34 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet ...

  7. javascript笔记3-面向对象的程序设计-创建对象

    javascript的对象:无序属性的集合,而每个属性可以包含基本值.对象或者函数.举例: var person = new Object(); person.name = "Nichola ...

  8. python的变量作用域

    import time global mark,sum def gaosi(Q): global sum,mark # 在 使用的时候防止隔离 也要声明一下 这个是全局变量 , 引用外面的值 sum+ ...

  9. Ajax中的eval函数的用法

    eval的定义和使用: Eval它是用来计算某个字符串,并且执行其中的JavaScript代码. 语法: 1) eval函数接受一个string这个参数,并且这个参数是必须的,这个参数就是要计算的这个 ...

  10. iOS ZBarSDK的基本使用:扫描

    1.首先使用Cocoapods导入库 ZBarSDK 2.敲代码: ViewController.h // // ViewController.h // erweima // // Created b ...