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. IT公司100题-3-求数组的最大子序列的和

    问题描述: 输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2 ...

  2. Xcode6中segue取消原push与modal(deprecated)

    xcode6 之后push 和modal 就被废弃了.只能用于ios8之前.在拖线的时候我们就可以看见. 这两个方法被废弃了,我们需要找到合适的方法来代替,这时候我们发现 show 和Present ...

  3. (转)iOS应用架构谈 view层的组织和调用方案

    前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...

  4. putty基本操作

    1,进入全屏 标题栏右键,菜单中就有full screen选项. 2,退出全屏 鼠标移到左上角,单击鼠标左键,就会跳出菜单,full screen勾去掉. 3,从putty中复制内容到剪切板 鼠标左键 ...

  5. HTML参考

    HTML Basic Document <html> <head> <title>Document name goes here</title> < ...

  6. tic/toc/cputime测试时间

    cputime测试代码运行时间可能不及tic/toc准确是众所周知的事情.本文并非旧话重提,而是期望起到抛砖引玉的效果,从而找到cputime与tic/toc内在的区别.望不吝赐教! 用tic/toc ...

  7. Android Studio 使用genymotion 模拟器运行app时 提示找不到任何设备

    原因是使用了genymotion 默认的Android toos .打开genymotion  选择设置  ADB  使用自己的SDKtools 选择Android Studio 使用的SDK位置就行 ...

  8. 去除hadoop启动时的警告

    hadoop启动的时候,会出现以下警告提示: 执行more start-all.sh查看该文件 但/libexec下不存在hadoop-config.sh文件,所以会执行bin/hadoop-conf ...

  9. 笔记本安装Win2012R2 心得(包含无线网卡+有线网卡驱动解决方法)

    笔记本:联想昭阳E47G 无线网卡安装方法: 系统安装完毕后将自动识别无线网卡驱动,但需要手动允许WLAN服务开启.(建议,如果是拿来办公或者家用,可以安装上桌面体验)不然,QQ发的截图双击都看不起. ...

  10. hdu 1069

    //Accepted 264 KB 0 ms //每种block只有三种方法,且每种放法至多放一次 //规定三条边的顺序后 //把所有的block按x递增排序,x相同则按y递增排序 //然后dp // ...