/*
解法一:使用链表从0实现栈,用min来存放最小值。
复杂的地方是,如果pop了最小的数,就要遍重新找到最小的数。
*/
public class MinStack {
List<Integer> list;
int min;
/** initialize your data structure here. */
public MinStack() {
list=new LinkedList<>();
min=Integer.MAX_VALUE;
} public void push(int x) {
if (x<min)
min=x;
list.add(x);
} public void pop() {
if (min==list.get(list.size()-1)){
min=Integer.MAX_VALUE;
for (int i=0;i<list.size()-1;i++){
if (list.get(i)<min)
min=list.get(i);
}
}
if (list.size()!=0)
list.remove(list.size()-1); } public int top() {
return list.get(list.size()-1);
} public int getMin() {
return min;
}
/*
解法二:使用Java的栈,并用一个辅助栈来存最小值。
*/
public class MinStack2{
Stack<Integer> stack;
Stack<Integer> helper;
/** initialize your data structure here. */
public MinStack2() {
stack=new Stack<>();
helper=new Stack<>();
} public void push(int x) {
stack.push(x);
if (helper.isEmpty()||x<=helper.peek())
helper.push(x);
} public void pop() {
if (!stack.isEmpty()){
int i=stack.pop();
if (i==helper.peek())
helper.pop();
}
} public int top() {
if (!stack.isEmpty())
return stack.peek();
throw new RuntimeException("stack is empty");
} public int getMin() {
if (!helper.isEmpty())
return helper.peek();
throw new RuntimeException("stack is empty");
}
} }

155--MinStack的更多相关文章

  1. [LeetCode] 155. minStack 设计最小栈

    注意:getMin()时间复杂度为O(1) 最原始的方法: class MinStack(object): def __init__(self): """ initial ...

  2. LeetCode No.154,155,156

    No.154 FindMin 寻找旋转排序数组中的最小值 II 题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7 ...

  3. leetcode算法学习----155. 最小栈(MinStack )

    下面题目是LeetCode算法155题: https://leetcode.com/problems/min-stack/ 题目1:最小函数min()栈 设计一个支持 push,pop,top 操作, ...

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

  5. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  6. 11/9 <Stack> 155 232 225

    155. Min Stack class MinStack { int min = Integer.MAX_VALUE; Stack<Integer> stack = new Stack& ...

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

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

  8. Java实现 LeetCode 155 最小栈

    155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...

  9. 【LeetCode】155. 最小栈

    155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...

  10. 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)

    李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...

随机推荐

  1. Linux—各种重要配置文件详解

    一./etc/profile文件详解(环境变量) 添加环境变量 .编辑profile文件 [root@localhost ~]# vi /etc/profile .在profile文件中添加如下内容 ...

  2. Linux 设备树详解【转】

    转自:http://www.pianshen.com/article/428276673/;jsessionid=D90FC6B215155680E0B89A6D060892D4 本文基于天嵌E9V3 ...

  3. CentOS 8安装体验

    这两天出来了,晚上爽一爽. 一,下载 http://ftp.sjtu.edu.cn/centos/8.0.1905/isos/x86_64/ 还是那7G左右的保险,没有minial版了,那个500m多 ...

  4. 8. Go语言—指针类型

    一.指针类型介绍 普通类型,变量存的就是值,也叫值类型. 获取变量的地址,用&,比如:var a int ,获取a的地址:&a 指针类型,变量存的是一个地址,这个地址存的才是值(指针存 ...

  5. ar 归档

    ar的常用用法见正文. 1.创建库文件 我 不知道怎么创建一个空的库文件.好在这个功能好像不是很需要.通常人们使用“ar cru liba.a a.o"这样的命令来创建一个库并把a.o添加进 ...

  6. 浅谈this指向问题

    链接地址:https://www.jianshu.com/p/34572435b5d0

  7. 《深度学习》圣经"花书"经验法则中文版!

    作者:Jeff Macaluso https://jeffmacaluso.github.io/post/DeepLearningRulesOfThumb/ 转自CVer,仅用作个人学习 当我在研究生 ...

  8. C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符

    #include <iostream> // overloading "operator == " outside class // == 是二元操作符 /////// ...

  9. 7.Java内存模型详解

    https://blog.csdn.net/qq_37141773/article/details/103138476 一.虚拟机 同样的java代码在不同平台生成的机器码肯定是不一样的,因为不同的操 ...

  10. x3

    #include<stdio.h> int main() { char ch; printf("输入一个字符:\n"); scanf("%c",&a ...