155--MinStack
/*
解法一:使用链表从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的更多相关文章
- [LeetCode] 155. minStack 设计最小栈
注意:getMin()时间复杂度为O(1) 最原始的方法: class MinStack(object): def __init__(self): """ initial ...
- LeetCode No.154,155,156
No.154 FindMin 寻找旋转排序数组中的最小值 II 题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7 ...
- leetcode算法学习----155. 最小栈(MinStack )
下面题目是LeetCode算法155题: https://leetcode.com/problems/min-stack/ 题目1:最小函数min()栈 设计一个支持 push,pop,top 操作, ...
- 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 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- 11/9 <Stack> 155 232 225
155. Min Stack class MinStack { int min = Integer.MAX_VALUE; Stack<Integer> stack = new Stack& ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java实现 LeetCode 155 最小栈
155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...
- 【LeetCode】155. 最小栈
155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...
- 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)
李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...
随机推荐
- SRDC - ORA-1548: Checklist of Evidence to Supply (Doc ID 1682693.1)
SRDC - ORA-1548: Checklist of Evidence to Supply (Doc ID 1682693.1) Action Plan 1. Execute srdc_db_u ...
- Linux同一机器设置多个IP2019-7-6
1.临时增加 1)先查看目前的网卡信息 [root@study ~]# ifconfigeno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAS ...
- metasploit篇
第一部分:基本使用 1.在kali中metasploit默认使用postgresql作为它的数据库: /etc/init.d/postgresql start 开机自启:update-rc.d pos ...
- Python正则表达式中re.S作用
re.S的作用: 不使用re.S时,则只在每一行内进行匹配,如果存在一行没有,就换下一行重新开始,使用re.S参数以后,正则表达式会将这个字符串看做整体,在整体中进行匹配 对比输出结果: import ...
- C++:map用法及元素的默认值
C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...
- layui增删改查
dao方法 package com.dao; import java.sql.SQLException; import java.util.List; import java.util.Map; im ...
- 【cf375】D. Tree and Queries(dsu on tree+线段树)
传送门 题意: 给出一颗以\(1\)为根的有根树,每个结点有个颜色\(c_i\). 之后要回答\(m\)组询问,每组询问包含\(v_i,k_i\),要回答以\(v_i\)为根的子树中,颜色出现次数不小 ...
- 创建Djongo需要改url的地方:
from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = ...
- [PHP] 阿里云 Composer 全量镜像
阿里云 Composer 全量镜像 镜像地址:https://mirrors.aliyun.com/composer/
- 研究是一门艺术 (韦恩·C·布斯, 格雷戈里·G·卡洛姆, 约瑟夫·M·威廉姆斯 著)
第一部分 研究,研究者与读者 前言: 开始一个研究计划 (已看) 第一章 以书面形式来思考 (已看) 第二章 与读者建立联系 第二部分 提问题,找答案 前言: 规划你的研究计划 第三章 从题目到问题 ...