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. MapReduce调度器

    1. 先进先出(FIFO)调度器 先进先出调度器是Hadoop的默认调度器.就像这个名字所隐含的那样,这种调度器就是用简单按照“先到先得”的算法来调度任务的.例如,作业A和作业B被先后提交.那么在执行 ...

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

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

  3. 【LeetCode】Jump Game (一维动态规划 + 线性扫描)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. MongoDB 聚合管道(aggregate)

    1.aggregate() 方法 我们先插入一些测试数据 { "_id" : ObjectId("5abc960c684781cda6d38027"), &qu ...

  5. es5 - array - concat

    /** * 描述:数组元素合并 * 使用:arr1.concat(arr2) * 参数:arr1.concat(arr2,arr3,arr...) * 说明: * 该concat方法创建一个新数组,该 ...

  6. 多用户角色权限访问模块问题”的解决思路( 位运算 + ActionFilterAttribute )

    如果你还是不太懂位运算,请看我的文章:那些年我们一起遗忘的位运算! 下面是我在这次项目中学习到的,我眼中的位运算的应用!主要是实现 通知的3个操作: 1.  置顶 2.  设为首页 3.  同时为 “ ...

  7. Python 遍历dict

    遍历dict 由于dict也是一个集合,所以,遍历dict和遍历list类似,都可以通过 for 循环实现. 直接使用for循环可以遍历 dict 的 key: >>> d = { ...

  8. 关于AWS的备份策略

    AWS有一个很强大的功能,就是snapshot,翻译过来就是对EBS进行快照.通俗的说,即是对整个硬盘进行完整的镜像备份.如此一来,在其中一台EC2挂掉的时候,我们迅速的另起一台EC2,并将通过快照恢 ...

  9. ubuntu——printk()函数总结,关于日志文件

    我们在使用printk()函数中使用日志级别为的是使编程人员在编程过程中自定义地进行信息的输出,更加容易地掌握系统当前的状况. 对程序的调试起到了很重要的作用. (下文中的日志级别和控制台日志控制级别 ...

  10. 自己动手写CPU之第四阶段(3)——MIPS编译环境的建立

    将陆续上传本人写的新书<自己动手写CPU>(尚未出版).今天是第13篇.我尽量每周四篇 4.4 MIPS编译环境的建立 OpenMIPS处理器在设计的时候就计划与MIPS32指令集架构兼容 ...