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. 魅族MX4的线控电路图

  2. 20.custom自定义线程池

    自定义线程池 1.若Executors工厂类无法满足需求,可以自己使用工厂类创建线程池,底层都是使用了); ThreadPoolExecutor threadPoolExecutor = new Th ...

  3. dedecms织梦 v5.5 两处跨站漏洞

    漏洞版本: dedecms织梦5.5 漏洞描述: 北洋贱队(http://bbs.seceye.org)首发 demo1:http://www.dedecms.com/plus/search.php? ...

  4. WebSocket原理分析

    Web应用的通信过程通常是客户端通过浏览器发出一个请求,服务器端接收请求后进行处理并返回结果给客户端,客户端浏览器将信息呈现.这种机制对于信息变化不是特别频繁的应用可以良好支撑,但对于实时要求高.海量 ...

  5. SQL Server更改排序规则的实现过程

    摘自: http://www.2cto.com/database/201112/115138.html 以下的文章主要向大家描述的是SQL Server更改排序规则的实现过程,以及在实现其实际操作过程 ...

  6. mysql5.0.x统计每秒增删改查替换数及系统每秒磁盘IO

    转载自:http://blog.chinaunix.net/uid-9370128-id-393082.html 1. mysql 5.0.x 统计每秒增,删,改,查,替换数  mysql 的show ...

  7. ubuntu docker方式部署docker registry v2

    生成自己签名的证书 生成签名的过程需要根据提示输入一些参数,需要注意的时Common Name的时候需要输入一个自己需要的域名,如果时内部域名记得访问的时候需要修改hosts. mkdir /data ...

  8. jquery翻页

    http://js.itivy.com/simplePagination.js/index.html#page-10 http://www.oschina.net/news/41941/7-html5 ...

  9. js 实现图片的无缝滚动

      js 实现图片的无缝滚动 CreateTime--2018年3月7日17:18:34 Author:Marydon 测试成功 <!DOCTYPE html> <html> ...

  10. Makefile之大型工程项目子目录Makefile的一种通用写法

    管理Linux环境下的C/C++大型项目,如果有一个智能的Build System会起到事半功倍的效果,本文描述Linux环境下大型工程项目子目录Makefile的一种通用写法,使用该方法,当该子目录 ...