12. Min Stack【medium】
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.
push(1)
pop() // return 1
push(2)
push(3)
min() // return 2
push(1)
min() // return 1
题意
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。
你实现的栈将支持push,pop 和 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】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
- 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 ...
随机推荐
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- 视频运行库AVICAP32.DLL说明收藏
视频运行库AVICAP32.DLL说明收藏2008-09-28 09:04 // ----------------------------------------------------------- ...
- datagridview 单元格类型转换【备忘】
datagridview 在设定列类型后,其下面所有行的该列都与设定的列类型相同. 在需要改变某一行的某个单元格时,遇到了一些问题,再次进行备忘: 之前在遇到该问题时参考别人的博客解决过,但是时间久 ...
- Python 字符串前面加'r'
在Python的string前面加上‘r’, 是为了告诉编译器这个string是个raw string,不要转意backslash '\' . 例如,\n 在raw string中,是两个字符,\和n ...
- 对于Ian的访谈,不少关于GAN的内容
文章链接如下: http://3g.163.com/dy/article/DD1GBSLF0511ABV6.html 里面提到胶囊网络,我找了这篇文章看了下: https://blog.csdn.ne ...
- cocos2d-x游戏开发 跑酷(八) 对象管理 碰撞检測
对象管理类的原理是这种: ObjectManager类是一个单例类,全局仅仅有一个对象实例存在.初始化的时候创建两个数组CCArray来保存金币和岩石.为什么要保存,由于在地图重载的时候.要销毁看不见 ...
- SQL语法 之 基本查询
一.语法结构 SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY ...
- linux系统中的DNS服务器介绍
http://lq2419.blog.51cto.com/1365130/1172269 DNS:Domain Name Service,linux上的DNS服务是基于一种软件BIND实现的.BIND ...
- 基于SSM + Redis的Shiro权限管理项目
概述 本教程结合SSM(SpringMVC + Mybatis)框架讲解Shiro,讲解的内容有自定义shiro拦截器,Shiro Freemarker标签,Shiro JSP标签,权限控制讲解. 详 ...
- 【剑指offer】面试题30:最小的K个数
import random def partition(data, start, end): if end <= start: return start index = random.randi ...