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. ubuntu php.ini文件位置

    mc@XJ > locate php.ini/etc/php5/cli/php.ini/etc/php5/fpm/php.ini

  2. C++-继承名称的掩盖

    /////////////////////////////////////////////////////////////////////////////// // // FileName : eff ...

  3. Spring对jdbc的支持

    Spring对jdbc技术提供了很好的支持. 体现在: 1)Spring对c3p连接池的支持很完善: 2)Spring对jdbc提供了JdbcTemplate,来简化jdbc操作: 1.使用步骤 1) ...

  4. 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK

    LEADTOOLS Video Streaming Module控件为您创建一个自定义的视频会议应用程序和工具提供所有需要的功能.软件开发人员可以使用Video Streaming Module SD ...

  5. 分享第一次使用ProcessOn心得

    最近朋友推荐了我一款在线作图工具ProcessOn,感受使用了几天感觉确实很不错,在这里给大家分享一下! ProcessOn应该算的上是第一款完全免费在线作图工具,之前用过国外有类似的,不过都是付费的 ...

  6. 为什么socket编程要用到多线程

    不得不佩服计算机先驱的设计:socket编程为什么需要多线程.如果只有一个ServerSocket线程,那么如下代码: public void start() throws Exception { S ...

  7. redis补充和rabbitmq讲解

    线程池: redis发布订阅: rabbitMQ: MySQL; python pymysql: python orm SQLAchemy: paramiko: 堡垒机: 1.线程池 1.1 cont ...

  8. 简单学习:repo入门

    一:关于repo repo是Google开发的用于管理Android版本库的一个工具,repo并不是用于取代git,而是用Python对git进行了一定的封装,简化了对多个Git版本库的管理.对于re ...

  9. OD调试4--绕过nag窗口

    先看一下程序的运行情况 先跳出了一个nag窗口 点确定 又跳出了一个NAG窗口,这是一些程序编写的时候常用的方法,设法让你购买正版软件, 于是今天呢,学会了四种绕过NAG的方法 我们先用OD加载进入这 ...

  10. Arrays Multi

    <!DOCTYPE html><html><body><?php$cars = array   (   array("Volvo",22, ...