题目

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;
};

GitHub测试程序源码

LeetCode(155) Min Stack的更多相关文章

  1. LeetCode(225) Implement Stack using Queues

    题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...

  2. 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 ...

  3. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  4. 新概念英语(1-55)The Sawyer family

    新概念英语(1-55)The Sawyer family When do the children do their homework? The Sawyers live at 87 King Str ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  8. 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 ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. 配置本地和远程maven仓库

    <mirrors><mirror> <id>alimaven</id> <name>aliyun maven</name> &l ...

  2. 企业级Web Nginx 服务优化

    企业级Web Nginx 服务优化 http://blog.51cto.com/search/result?q=%E4%BC%81%E4%B8%9A%E7%BA%A7Web+Nginx+%E6%9C% ...

  3. [RDL]中多行组列组占比报表制作

    结果如下: 生意额占比表达式:=iif(Fields!生意额.Value is nothing,"",Fields!生意额.Value/sum(Fields!生意额.Value, ...

  4. docker安装软件

    镜像相关命令 1.搜索镜像 # docker search java 可使用 docker search命令搜索存放在 Docker Hub(这是docker官方提供的存放所有docker镜像软件的地 ...

  5. UIScrollView使用stoboard自动布局

    使用stoboard布局 scrollView 是有点麻烦的,首先我们往往约束好一个 scrollView 然后在添加子控件,此时都会报错,原因是, scrollView必须确定滚动范围 然后在使用V ...

  6. CDN加速服务

    CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将JS库存放在服务器单机上相比,CDN公共库更加稳定.高速.一般的CDN公共库都会包含全球所有最流行的开源JavaScrip ...

  7. python爬虫之路——初识基本页面构造原理

    通过chrome浏览器的使用简单介绍网页构成 360浏览器使用右键审查元素,Chrome浏览器使用右键检查,都可查看网页代码. 网页代码有两部分:HTML文件和CSS样式.其中有<script& ...

  8. ABAP Development Tools的语法高亮实现原理

    ABAP Development Tools的前端是Java,根本识别不了ABAP.那么在ADT里的ABAP语法高亮是如何实现的? 第一次打开一个report时,显示在ADT里的代码是没有任何语法高亮 ...

  9. db2疑难解决

    http://www-01.ibm.com/support/knowledgecenter/?lang=zh#!/SSEPGG_9.5.0/com.ibm.db2.luw.messages.sql.d ...

  10. [大坑]Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR

    报错tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTER ...