/*
解法一:使用链表从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 PXE + Kickstart 自动装机

    大规模装机时,使用无人值守装机便可大大简便人工操作,提高效率. PXE 网络安装 配置双网卡 这里ens33为nat网络,ens37为仅主机网络,配置ens37 [root@localhost ~]# ...

  2. 关于 from scipy.misc import imread, imresize, imsave 报错的问题

    使用 from scipy.misc import imread, imresize, imsave 时出现报错,查找后发现新版本的Scipy不再包含imread,imresize,imsave,需要 ...

  3. 【洛谷P4589】[TJOI2018]智力竞赛(二分+最小链覆盖)

    洛谷 题意: 给出一个\(DAG\),现在要选出\(n+1\)条可相交的链来覆盖,最终使得未被覆盖的点集中,权值最小的点的权值最大. 思路: 显然最终的答案具有单调性,故直接二分答案来判断: 直接将小 ...

  4. 【洛谷P3329】 [ZJOI2011]最小割(最小割树)

    洛谷 题意: 给出一个无向图,之后有\(q,q\leq 30\)组询问,每组询问有一个\(x\),回答有多少点对\((a,b)\)其\(a-b\)最小割不超过\(x\). 思路: 这个题做法要最小割树 ...

  5. Java常识2

    JDK 的下载 安装 下载 官网 github安装 傻瓜式安装 JDK .JRE 注意问题 安装软件的 路径不能包含中文 空格 path环境变量 windows操作系统执行命令是所要搜寻的路径为什么要 ...

  6. goroutine使用

    Goroutine是建立在线程之上的轻量级的抽象.它允许我们以非常低的代价在同一个地址空间中并行地执行多个函数或者方法.相比于线程,它的创建和销毁的代价要小很多,并且它的调度是独立于线程的.在gola ...

  7. SUDO_KILLER可以帮助你识别并利用错误的Sudo规则与配置

    工具概述 SUDO_KILLER这款工具可以帮助我们通过多种渠道利用SUDO来在Linux环境下实现提权.该工具能够识别目标操作系统版本,并发现环境中sudo规则的错误配置.安全漏洞,以及不安全的代码 ...

  8. pytest 常见用法

    前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...

  9. Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

    Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...

  10. Unsupervised Attention-guided Image-to-Image Translation

    这是NeurIPS 2018一篇图像翻译的文章.目前的无监督图像到图像的翻译技术很难在不改变背景或场景中多个对象交互方式的情况下将注意力集中在改变的对象上去.这篇文章的解决思路是使用注意力导向来进行图 ...