/*
解法一:使用链表从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. LCD RGB 控制技术 时钟篇(下)【转】

    上一篇博文,我们介绍了LCD RGB控制模式的典型时钟.那么这一片我们要详细的去讨论剩下的细节部分. 我们先回顾一下之前的典型时序图 在这个典型的时序图里面,除了上篇博文讲述的HSYNC VSYNC ...

  2. Matplotlib 日期格式转换

    官网链接:https://matplotlib.org/api/dates_api.html#matplotlib.dates.date2num import numpy as np import d ...

  3. fiddler---Fiddler修改数据信息

    在测试的过程中,可能我们会遇到需要修改一些数据查看请求返回内容是如何的,刚好Fiddler也可以满足我们的要求,Fiddler不仅可以抓包还可以修改包的内容 Fiddler修改数据原理 Fiddler ...

  4. 2. java 运算符

    运算符 一.算术运算符 1. 四则与取模 + - * / % ++ -- (1) 单独使用++/--,前++和后++没有任何区别. (2) 混合使用,有区别 ①如果是前++,那么变量立刻马上 +1,然 ...

  5. glibc-static

    yum install glibc-static yum install libstdc++-static

  6. 十一,专著研读(CART算法)

    十一,专著研读(CART算法) CART称为分类回归树,既能用于分类也能用于回归.使用二元切分方法处理连续型变量,给定特定值,如果特征值大于给定值就走左子树,否则走右子树. CART算法步骤 决策树生 ...

  7. Pwn-level3

    题目地址 https://dn.jarvisoj.com/challengefiles/level3.rar.2047525b05c499c9dd189ba212bba1f8 借鉴 https://w ...

  8. aiohttp_spider

    aiohttp_spider_def: import asyncio import re import aiohttp import aiomysql from pyquery import PyQu ...

  9. imutils.path

    from imutils import paths # 要在哪条路径下查找 path = '...' # 查找图片,得到图片路径 imagePaths = list(imutils.paths.lis ...

  10. [PHP] 再续 Laravel 5.5 接口 跨域问题 【终极暴力解决办法】

    上文中提到 Laravel5.5 使用 laravel-cors 实现 Laravel 的跨域配置 用插件来跨域 此方法能解决一部分api 请求问题 但我碰到的是 接口 请求size 超过10k,导致 ...