LeetCode(155) Min Stack
题目
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Subscribe to see which companies asked this question。
分析
实现可求最小值的栈。
该特殊栈除了具有普通栈先进后出的特点,还必须可以有getMin函数求得实时的栈中最小元素。
利用一个辅助栈,保存当前元素对应栈的最小值。
AC代码
class MinStack {
public:
void push(int x) {
data.push(x);
if (minData.empty())
minData.push(x);
else{
if (minData.top() > x)
minData.push(x);
else
minData.push(minData.top());
}
}
void pop() {
data.pop();
minData.pop();
}
int top() {
return data.top();
}
int getMin() {
return minData.top();
}
private:
stack<int> data;
stack<int> minData;
};
LeetCode(155) Min Stack的更多相关文章
- LeetCode(225) Implement Stack using Queues
题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- 新概念英语(1-55)The Sawyer family
新概念英语(1-55)The Sawyer family When do the children do their homework? The Sawyers live at 87 King Str ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- 讲明白combineByKey()算子,不是谈源码
简单介绍 combineByKey()是最通用的对key-value型rdd进行聚集操作的聚集函数(aggregation function).类似于aggregate(),combineByKey( ...
- mysql 存储过程变量及循环的使用
1.用游标循环 BEGIN -- 定义变量 -- 定义done DECLARE done INT; -- 定义 ammeter_id_bl DECLARE ammeter_id_bl DOUBLE; ...
- PHP正则表达式 - 附录(常用正则表达式)
常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...
- 用Node+wechaty写一个爬虫脚本每天定时给女(男)朋友发微信暖心话
wechatBot 微信每日说,每日自动发送微信消息给你心爱的人 项目介绍 灵感来源 在掘金看到了一篇<用Node + EJS写一个爬虫脚本每天定时女朋友发一封暖心邮件>后, 在评论区偶然 ...
- html5 03
HTML03 一. 表单标签 <form></form> 常用属性 Action 跳转到什么页面 Method 以什么模式提交 Get Url有长度限制 IE6.0 url ...
- jsp页面file标签上传图片以及blob类型数据库存取。
我的jsp页面表单如下: <form name="form1" action="/YiQu/AddUserServlet?jurisdiction=1" ...
- Autoit3 如何捕足控件
以任务管理器为例,在命令行提示符下输入taskmgr.exe 接下来识别这个窗口上的控件 在AU3 中提供了一个捕捉控件的工具是Au3Info.exe 这里记录了控件的标题,控件的类型,控件的坐标和 ...
- c#在不安装Oracle客户端的情况下与服务器上的Oracle数据库交互
概述: C#通过使用ADO的方式在未安装Oracle数据库的前提下,客户端程序远程访问服务器,会出现:“System.Data.OracleClient 需要 Oracle 客户端软件 8. ...
- json格式引起的 parse 报错
json格式: {"ID":"4","PICDATAS":"[{"picDevNo": 1},{"p ...
- 如何处理错误消息Please install the gcc make perl packages
如何处理这行错误消息? Please install the gcc make perl packages from your distribution. 执行命令行:yum install gcc ...