[抄题]:

实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。

你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。

[思维问题]:

想不到,脑洞不够大

[一句话思路]:

用一个minstack来辅助实现

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 主函数中,数据结构为类+名 类是Stack<Integer>
  2. minStack.empty() == true 或者isempty都可以
  3. pop分为两种情况:二者peek(不是peak)相等的、不相等的; 方法加点来用

[二刷]:

  1. push可能有空的情况,要注意

[总结]:top只是看一看,peek也只是看一看。所以top里面包含peek

[复杂度]:Time complexity: O(1) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack; public MinStack() {
// do intialization if necessary
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
} /*
* @param number: An integer
* @return: nothing
*/
public void push(int number) {
// write your code here
stack.push(number);
if (minStack.isEmpty()) {
minStack.push(number);
}
else {
if (number <= minStack.peek()) {
minStack.push(number);
}
}
} /*
* @return: An integer
*/
public int pop() {
// write your code here
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
return stack.pop();
} /*
* @return: An integer
*/
public int min() {
// write your code here
return minStack.peek();
}
}
class MinStack {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>(); /** initialize your data structure here. */
public MinStack() {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>();
} public void push(int x) {
//stack: push
stack.push(x);//s:2 //min stack:push when it's null or minimum
if (minStack.isEmpty()) {
minStack.push(x);
} else if (x <= minStack.peek()) {
minStack.push(x); //ms:2
}
} public void pop() {
//stack:get, min stack: pop when it's mean
if (minStack.peek().equals(stack.peek())) {
minStack.pop(); //both 2, ms pop 2
}
stack.pop(); //s pop 2
} public int top() {
return stack.peek(); //return s pop 2
} public int getMin() {
return minStack.peek(); //return ms pop 2
}
} /**
* 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();
*/

解法2: 只用1个stack

[抄题]:

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不懂题目意思:设计新的数据结构。原生的stack中本来就有push pop peek方法

[一句话思路]:

刷新最小值时 stack中存2个 留着最小值备胎。否则只存1个 要走就走,下面一层还是最小值。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. pop一个元素之后把备胎也pop出去,以便于下次的pop, 不留痕迹
  2. class中声明后,MinStack()方法用于建立新的数据类型(真心是没话可说了)

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

备胎需要斩草除根

[复杂度]:Time complexity: O(1) Space complexity: O(n)

都是立存立取的

[英文数据结构或算法,为什么不用别的数据结构或算法]:

原生的stack中本来就有push pop peek方法

[关键模板化代码]:

[其他解法]:

2个stack

[Follow Up]:

[LC给出的题目变变变]:

716. Max Stack 2个stack

[代码风格] :

带最小值操作的栈 · Min Stack的更多相关文章

  1. lintcode12 带最小值操作的栈

    实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 建一个栈helpStack,用来存放从 ...

  2. LintCode-12.带最小值操作的栈

    带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 注意事项 如果堆栈中 ...

  3. LintCode 12.带最小值操作的栈(两种方法实现)

    题目描述 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 样例 如下操作:push(1 ...

  4. [LintCode] 带最小值操作的栈

    class MinStack { public: MinStack() { // do initialization if necessary } void push(int number) { // ...

  5. LeetCode 155:最小栈 Min Stack

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

  6. [Swift]LeetCode155. 最小栈 | Min Stack

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

  7. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  8. LeetCode 刷题笔记 155. 最小栈(Min Stack)

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

  9. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

随机推荐

  1. tornado.gen 模块解析

    转自:http://strawhatfy.github.io/2015/07/22/Tornado.gen/ 引言 注:正文中引用的 Tornado 代码除特别说明外,都默认引用自 Tornado 4 ...

  2. MySQL ALTER讲解

    当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@host# mysql -u r ...

  3. 教Alexa看懂手语,不说话也能控制语音助手

    Alexa.Siri.小度……各种语音助手令人眼花缭乱,但这些设备多是针对能力健全的用户,忽略了听.说能力存在障碍的人群.本文作者敏锐地发现了这一 bug,并训练亚马逊语音助手 Alex 学会识别美式 ...

  4. SQL Server数据库定时备份解决方案

    SQL Server数据库定时备份解决方案 1.本方案采用软件为:SQLBackupAndFTP 10.0.3 版本,压缩包自带注册机,请自行破解. 2.软件截图如下: 3.功能说明:自动定时备份相关 ...

  5. 【Flutter】Flutter 一些常用库

    Flutter社区和资源传送门 新: 慕课网<Flutter入门与案例实战>   |   中文网<Flutter实战>电子书 字体图标生成 http://fluttericon ...

  6. php读取word里面的内容antiword

    其实是现在一个linux下的扩展 1 先安装  antiword yum antiword install 2 写测试php代码 header("Content-type: text/htm ...

  7. 获取DataView行数据

      1.  dv.Table.Rows[0]["price"].ToString();这种方法虽然很长,但意思很清晰. 2.  dv[0]["price"].T ...

  8. jackson的小知识

  9. The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced

    springmvc mybatis整合,遇到错误:The type org.springframework.dao.support.DaoSupport cannot be resolved. It ...

  10. Unable to open file '.RES'

    Unable to open file '.RES' 另存工程,带来的隐患,工程图标也改不了. 搜索发现源码里某个man.cpp里带了prgram  resource aaa.res,换成新工程文件名 ...