Implement a stack with min() function, which will return the smallest number in the stack.

It should support push, pop and min operation all in O(1) cost.

Notice

min operation will never be called if there is no number in the stack.

 
Example

push(1)
pop() // return 1
push(2)
push(3)
min() // return 2
push(1)
min() // return 1

题意

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

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

解法一:

 class MinStack {
public:
stack<int> stk, minstk; MinStack() {
// do intialization if necessary } /*
* @param number: An integer
* @return: nothing
*/
void push(int number) {
stk.push(number);
if (minstk.empty() || number <= minstk.top()) {
minstk.push(number);
}
} /*
* @return: An integer
*/
int pop() {
int top = stk.top();
stk.pop();
if (top == minstk.top()) {
minstk.pop();
}
return top;
} /*
* @return: An integer
*/
int min() {
return minstk.top();
}
};

思路如下:

1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个;

2. pop时若两个stack的最上面值相等则都pop, 不等则只pop第一个stack, 但是都得返回第一个stack的pop值;

3. min时只返回第二个stack的peek值。

解法二:

 class MinStack {
public:
MinStack(){
// do initialization if necessary
} void push(int number) {
s.push(number);
m[number]++;
it = m.begin();
} int pop() {
int res = s.top();
s.pop(); if (res == it->first) {
(it->second)--; if (it->second == ) {
m.erase(it);
it=m.begin();
}
} else {
m.erase(res);
} return res;
} int min() {
return it->first;
} private:
stack<int> s;
map<int,int> m;
map<int,int>::iterator it;
};

利用map的排序,参考@知之可否 的代码

12. Min Stack【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. node最简单的升级

    1.安装n插件 npm install -g n //全局安装 2.升级 n stable //升级 3.packjson升级 npm i -g npm-upgrade 4.升级 npm-upgrad ...

  2. CRF,没完全看懂

    这篇文章,感觉讲的还比较浅显易懂: http://www.sohu.com/a/207085690_206784

  3. uva 10994 - Simple Addition(规律)

    题目链接:uva 10994 - Simple Addition 题目大意:给出l和r,求∑(l≤i≤r)F(i), F(i)函数题目中有. 解题思路:由两边向中间缩进,然后l和r之间的数可以按照1~ ...

  4. EChart处理三维数据做图表、多维legend图例处理

    处理三维数据做图表,比如返回的数据就是一个个list,list里面某几个数据同属于一个维度,项目中的实例效果如下: 上面的khfx会有多个,比如db1.db2.db3等,下面的那些数据也会变化,目前需 ...

  5. qt study 元对象,属性和反射编程

    所谓反射,就是指对象成员的自我检查,使用反射编程(reflective programming),就可以编写出通用的操作,可以对具有不同结构的类进行操作. QMetaObject 元对象模式,描述一个 ...

  6. 用rpm安装软件的常用步骤

    假设软件叫software.rpm 1.安装前,查看是否安装过用 rpm -q software 2.安装时,用 rpm -ivh software.rpm 3.安装后想删除,用 rpm -e sof ...

  7. Toast.makeText 方法出错 java.lang.RuntimeException

    接手以前同事留下的代码,今天突然出现了一个bug: java.lang.RuntimeException: Can't create handler inside thread that has no ...

  8. 一次寻找IBatisNet事务bug的过程

    本文的上下文环境 操作系统:Win7 x64 Professional 开发工具:Visual Studio 2017   语言:C# 数据库ORM:IBatisNet 1.6.2 一.前言 这个项目 ...

  9. java.sql.SQLException: Data truncation: Truncated incorrect DOUBLE value

    mysql 报这个异常:java.sql.SQLException: Data truncation: Truncated incorrect DOUBLE value update 表名 set c ...

  10. setup factory 打包VB 工程

    setup factory 使用起来很简单你可以如下:1.你把你刚编译出来的exe和相关的资源文件复制到某一空目录下.把exe文件添加到setup factory里之后,在列表里右键,属性里面可以设置 ...