题意:模拟一个最小栈,可以push,pop,top,和返回栈中最小值。

思路:已经忘了栈是怎么构建的了,晕···尝试了半天,错误,发现直接用stack数据结构来做最方便,再用一个栈来存最小值。值得注意的是当pop时最小值栈也要pop。

代码:

stack<int> Data, Min;
void push(int x)
{
Data.push(x);
if(Min.empty() || Min.top() > x)
Min.push(x);
else
Min.push(Min.top()); } void pop()
{
Data.pop();
Min.pop();
} int top()
{
if(!Data.empty())
return Data.top();
} int getMin()
{
if(!Min.empty())
return Min.top();
}

leetcode155 Min Stack的更多相关文章

  1. 第30题:LeetCode155. Min Stack最小栈

    设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...

  2. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  3. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  4. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  6. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  7. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  8. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  9. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

随机推荐

  1. 超详细的Java面试题总结(三)之Java集合篇常见问题

    List,Set,Map三者的区别及总结 List:对付顺序的好帮手 List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set:注重独一无二的性质 不允许重复的集合.不会有多个元 ...

  2. 暑假集训——cf热身赛部分题有感加其题解

    刚刚开始集训,集训队队长暂时还没有拉专题,而是拉了部分codeforces上过题人数在2000左右的题组成了一场热身赛(其实就是一场练习),花了一天时间终于把它刷完了,其中很多题让我学到了很多骚操作, ...

  3. TOJ 1049 Jesse's problem (最短路 floyd)

    描述 All one knows Jesse live in the city , but he must come to Xiasha twice in a week. The road is to ...

  4. js_一个简单的30分钟循环倒计时

    吐槽段: 需求的变更是千变万化的,至少在你说服和你打交道的那位谁谁谁之前. 创业公司就是这样,产品经理一个想法,就是改改改,管你改起来复杂不复杂,在他们眼里都是非常简单的. 今天的一个小改动需求,把活 ...

  5. linux下暴力破解工具hydra【转】

    一.简介 Number one of the biggest security holes are passwords, as every password security study shows. ...

  6. wce.exe getpass.exe 读取密码

    http://www.ampliasecurity.com/research/wce_v1_4beta_x32.zip http://www.ampliasecurity.com/research/w ...

  7. python基础===多线程

    https://www.cnblogs.com/wj-1314/p/8263328.html threading 模块 先上代码: import time, threading def loop(): ...

  8. Linux时间子系统之一:clock source(时钟源)【转】

    转自:http://blog.csdn.net/droidphone/article/details/7975694 clock source用于为linux内核提供一个时间基线,如果你用linux的 ...

  9. Windows 10又现新Bug,24核心竟卡成蜗牛

    Windows 10又现新Bug,24核心竟卡成蜗牛 https://news.cnblogs.com/n/573996/

  10. 1003: FFF团的情侣活动--课程作业--找出N个数字中唯一出现奇数次的数

    1003: FFF团的情侣活动 Time Limit: 1 Sec  Memory Limit: 2 MB Description 圣诞节快到了,Water作为大FFF团团长,组织许多对情侣进行电影院 ...