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. 运行Hadoop的示例程序WordCount-Running Hadoop Example

      In the last post we've installed Hadoop 2.2.0 on Ubuntu. Now we'll see how to launch an example ma ...

  2. 如何查找python安装包的路径site-packages?

    使用命令: python -m site python -m site --user-site 注意当查看指定版本的python的安装包时,需要指定python版本,比如python2.7.15 -m ...

  3. POJ 3069 Saruman&#39;s Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6688   Accepted: 3424 De ...

  4. apache kafka监控系列-KafkaOffsetMonitor(转)

    原文链接:apache kafka监控系列-KafkaOffsetMonitor 概览 最 近kafka server消息服务上线了,基于jmx指标参数也写到zabbix中了,但总觉得缺少点什么东西, ...

  5. Spring持久化

    1. Spring的DAO理念 Spring提供了一套抽象的DAO类,供开发者扩展,这有利于以统一的方式操作各种DAO技术,如JDO.JDBC等,这些抽象DAO类提供了设置数据源及相关辅助信息的方法, ...

  6. @ERR Unsupported CONFIG parameter: notify-keyspace-events

    Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: noti ...

  7. 1 cocos2dx源码分析-程序启动与主循环

        1 启动   在iOS系统中,由main函数启动默认调用了AppController main.m NSAutoreleasePool * pool = [[NSAutoreleasePool ...

  8. POJ 2046 Gap 搜索- 状态压缩

    题目地址: http://poj.org/problem?id=2046 一道搜索状态压缩的题目,关键是怎样hash. AC代码: #include <iostream> #include ...

  9. vue项目启动出现cannot GET /服务错误

    出现 Cannot GET/:  控制台中并没有报错:npm run dev命令行窗口也没有报错.  原因 在网上查了一堆,发现这个问题还挺多呢,而且各个回答的解决方式都竟然有许多不同… 于是把能改的 ...

  10. Android -- 消息处理机制源码分析(Looper,Handler,Message)

    android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因 ...