leetcode155 Min Stack
题意:模拟一个最小栈,可以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的更多相关文章
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
随机推荐
- 【usaco-Earthquake, 2001 Open】 0-1分数规划 & 最优比率生成树
题意:给定n个点m条边,一开始这些边全都是断的,要修一些边使得n个点全部联通.修完一共可以得到F元,修一条边有成本di和时间ti,要使得 得到的钱数 / 总时间 这个比值最大. 参考资料: 红线内的内 ...
- 【BZOJ2288】生日礼物 [贪心]
生日礼物 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ftiasch 18岁生日的时候, ...
- Codeforces 321E Ciel and Gondolas
传送门:http://codeforces.com/problemset/problem/321/E [题解] 首先有一个$O(n^2k)$的dp. # include <stdio.h> ...
- 【51NOD-5】1293 球与切换器
[算法]DP [题解]f[i][j][0]表示在i,j位置往下走的球数,f[i][j][1]表示在i,j位置往右走的球数,经过i,j的球若为-1则(num+1)/2往下,其余往右.+1类似. 转移见代 ...
- 01-UIScrollView01-大图片展示
源代码下载链接:01-UIScrollView01-大图片展示.zip283.7 KB // // MJViewController.m // 01-UIScrollView01-大图 ...
- list互转datatable 支持Nullable转换
/// <summary> /// list转datatable /// </summary> /// <param name="list">& ...
- dokuwiki安装部署
dokuwiki的地址:https://www.dokuwiki.org/dokuwiki# 1.部署dokuwiki 在D:\xampp\htdocs(xampp安装目录)新建一个doku文件夹,把 ...
- 解决不走onActivityResult方法
最近在开发公司项目,在使用startActivityForResult关联俩个Activity中,发现A跳转到B,B设置setResult之后,A没有执行onActivityResult,查找一下,发 ...
- Part2-HttpClient官方教程-Chapter3-HTTP状态管理
ps:近日忙于课设与一个赛事的准备....时间真紧啊~~ 最初,HTTP被设计为一种无状态的,面向请求/响应的协议,它并没有为跨越多个逻辑相关的请求/响应交换的有状态会话做出特殊规定.随着HTTP协议 ...
- mssql手工注入2
--+ 先说一些函数的说明: substring(str,start,len) 截取字符串的作用,第一个参数为要截取的字符串,第二个参数为从哪里开始截取,第三个参数为截取的长度 ascii(char) ...