题目

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. PHP函数生成随机数

    通常情况下,当我们要生成一个随机字符串时,总是先创建一个字符池,然后用一个循环和mt_rand()或rand()生成php随机数,从字符池中随机选取字符,最后拼凑出需要的长度,代码如下: <?p ...

  2. webpack.config.js====插件html-webpack-plugin

    1. 安装 cnpm install html-webpack-plugin --save-dev 2. webpack.config.js中使用 const htmlWebpackPlugin = ...

  3. w3c万维网的介绍和html基本构成

    怎么与浏览器交互? 1.鼠标 2.键盘输入 w3c标准: 中文名:万维网联盟!外文名:world wide web cansortium万维网联盟创建于1994年,是web技术领域最具权威个影响的国际 ...

  4. Country roads take me home, to the place I belong.

    Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.

  5. hadoop完全分布式模式搭建和hive安装

    简介 Hadoop是用来处理大数据集合的分布式存储计算基础架构.可以使用一种简单的编程模式,通过多台计算机构成的集群,分布式处理大数据集.hadoop作为底层,其生态环境很丰富. hadoop基础包括 ...

  6. html5 03

    HTML03 一. 表单标签 <form></form> 常用属性 Action 跳转到什么页面 Method  以什么模式提交 Get Url有长度限制 IE6.0 url ...

  7. Android自定义组件系列【17】——教你如何高仿微信录音Toast

    一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...

  8. Docker学习资料汇总

    一.Docker问答录(100问)  链接:https://blog.lab99.org/post/docker-2016-07-14-faq.html 二.Windows 10 如何安装Docker ...

  9. Python+selenium之调用JavaScript

    webdriver提供了操作浏览器的前进和后退的方法,但是对于浏览器公东条并没有提供相应的操作方法.于是就需要借助JavaScript来控制浏览器的滚动条.webdriver提供了execute_sr ...

  10. 菜鸟的数据库实战-4-数据阅读器SqlDataReader

    老铁们大家好啊,我是菜鸟思奎,今天我学习的是数据库和前端的连接用到的字符串,如果有什么纰漏希望大家在评论区指正.阿里嘎多. 我的环境是Visual Studio 2008 + Microsoft SQ ...