题目

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. (转)mysqldump: Got error: 1556: You can't use locks with log tables.

    mysqldump: Got error: 1556: You can't use locks with log tables. 原文:http://blog.51cto.com/oldboy/112 ...

  2. 5. 把一幅彩色图像的H、S、I分量单独显示。

    #include <cv.h> #include <highgui.h> # include <math.h> #define M_PI 3.1415 void R ...

  3. MySQL 实现字符串换行

    target_describe字段值中包含 :[ 这两个特殊的字符 ,想要在字符之间加换行 需要插入CHAR(10) ),'[')) UPDATE ew_pm_project_red_detail S ...

  4. This file's format is not supported or you don't specify a correct format. 解决办法

    string path = @"c:\请假统计表.xlsx"; Workbook workBook = new Workbook(); workBook.Open(path); A ...

  5. 仙人掌(cactus)

    题目描述LYK 在冲刺清华集训(THUSC)!于是它开始研究仙人掌,它想来和你一起分享它最近研究的结果.如果在一个无向连通图中任意一条边至多属于一个简单环(简单环的定义为每个点至多经过一次),且不存 ...

  6. log4j.properties配置详情

    log4j: log for java 是Apache的一个开源项目! 00.将我们的日志信息,输出到指定的位置(控制台   文件中) 01.我们可以控制每一条日志的输出格式 02.我们设置日志信息的 ...

  7. go语言简单的soap调用方法

    package main import ( "bytes" "encoding/xml" "fmt" "io" &quo ...

  8. 工作方法-scrum+番茄工作法

    1.产品和开发团队近期的工作分析和安排,使用scrum. 产品的工作:通过product backlog来列出 开发团队近期的工作安排:通过sprint backlog来列出,由个人认领,并估算(优先 ...

  9. MySql自动默认时间及更新时间

    注意:5.7 才能用类型为datetime的字段实现 `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `modifie ...

  10. Outlook 0x800CCC1A 错误

    使用POP3帐户时,您可能在Outlook 2013/2016中看到以下错误.我在Exchange Server 2013环境中遇到此问题,在Windows 8.1上运行的Microsoft Outl ...