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. mac下python2.x和python3.x的安装方法和升级方法/卸载

    一.首先问个问题,我们为什么要升级python2.x或者python3.x的版本? 一个是低版本会有些bug:或者功能问题,或者安全问题等,另外高版本会引进一些新的功能,也会废弃一些老的功能. 可以通 ...

  2. Spark应用程序运行的日志存在哪里(转)

    原文链接:Spark应用程序运行的日志存在哪里 在很多情况下,我们需要查看driver和executors在运行Spark应用程序时候产生的日志,这些日志对于我们调试和查找问题是很重要的. Spark ...

  3. linux系统编程:守护进程详解及创建,daemon()使用

    一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而且提供某种服务,不是对整个 ...

  4. 【帧动画总结】AnimationDrawable Frame

    Drawable Animation 开发者文档 位置:/sdk/docs/guide/topics/graphics/drawable-animation.html Drawable animati ...

  5. js中的this基础

    this在js中的地位可以说是相当高了,本文介绍下this的基本相关情况,以后还会慢慢介绍 在页面中aler(this)//this的指向是window 在DOM操作中this的指向是当前发生事件的对 ...

  6. PHP快速入门 如何下载和安装

    1 从该网站下载 http://www.appservnetwork.com/ 2 双击安装,注意安装路径和安装组件 按下面的填写,端口不要和IIS冲突,字符编号为UTF-8 安装完成之后,注意安装文 ...

  7. Google POI下载工具破解之路

    我是GIS初学者,爱好二次开发,像初恋一样.最近对编译感兴趣,每当成功获取一点信息,就有一种快感,感觉马上就要成功了……其实,还早! 01.初次反编译 今天在微创业工作室找到了Google POI下载 ...

  8. Android调用系统拍照裁剪和选图功能

    最近项目中用到修改用户头像的功能,基本上都是模板代码,现在简单记录一下. 调用系统拍照 private fun openCamera() { //调用相机拍照 // 创建File对象,用于存储拍照后的 ...

  9. css3和html5的学习

    本文是此链接的源代码.http://www.imooc.com/learn/77 关于的html5的使用: transition----含义是:过渡的过程,能够对各种属性设置变化. 有5中过渡的形式: ...

  10. 解决github push错误The requested URL returned error: 403 Forbidden while accessing(转)

    github push错误: git push error: The requested URL returned error: 403 Forbidden while accessing https ...