LeetCode 最小栈
题目链接:https://leetcode-cn.com/problems/min-stack/
题目大意
分析
代码如下
class MinStack {
public:
/** initialize your data structure here. */
stack< int > sk;
stack< int > minsk; // 从栈底到栈顶递增的单调栈
MinStack() {
}
void push(int x) {
sk.push(x);
if(!minsk.empty() && x > minsk.top()) minsk.push(minsk.top());
else minsk.push(x);
}
void pop() {
//assert(!sk.empty() && !minsk.empty());
sk.pop();
minsk.pop();
}
int top() {
//assert(!sk.empty());
return sk.top();
}
int getMin() {
//assert(!minsk.empty());
return minsk.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
LeetCode 最小栈的更多相关文章
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
- LeetCode初级算法--设计问题02:最小栈
LeetCode初级算法--设计问题02:最小栈 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode OJ:Min Stack(最小栈问题)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode 腾讯精选50题--最小栈
题目很简单,实现一个最小栈,能够以线形的时间获取栈中元素的最小值 自己的思路如下: 利用数组,以及两个变量, last用于记录栈顶元素的位置,min用于记录栈中元素的最小值: 每一次push,都比较m ...
- Java实现 LeetCode 155 最小栈
155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...
- 【LeetCode】155. 最小栈
155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...
随机推荐
- 使用Guzzle执行HTTP请求
Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上.Guzzle提供了简单的接口,构建查询语句.POST请求.分流上传下载大文件.使用HTTP cookies ...
- VTemplate模板引擎的使用--进阶篇
1.<vt:template>与<vt:include>标签的不同 <vt:template>和<vt:include> 标签都包含file属性,如果这 ...
- Nginx网络架构实战学习笔记(四):nginx连接memcached、第三方模块编译及一致性哈希应用
文章目录 nginx连接memcached 第三方模块编译及一致性哈希应用 总结 nginx连接memcached 首先确保nginx能正常连接php location ~ \.php$ { root ...
- javascript闭包实现缓存小案例
/* * 闭包实现缓存 * 属性:有个键--值 --->所以可以将缓存数据存放在一个对象中 * 方法:缓存存储 setCache * 缓存的获取 getCache * */ function ...
- shell 删除除匹配字符串之外的所有文件夹
file_dir=` -maxdepth - type d`for dir in $file_dirdo file_name=`basename $dir` if [ $file_name != &q ...
- Dubbo 微服务系列(03)服务注册
Dubbo 微服务系列(03)服务注册 [TOC] Spring Cloud Alibaba 系列目录 - Dubbo 篇 1. 背景介绍 图1 Dubbo经典架构图 注:本图来源 Dubbo官方架构 ...
- 挂载时出现mount: RPC: Unable to receive; errno = Connection refused错误的解决方法
当我们在做NFS开发板下挂载时,经常会出现mount: RPC: Unable to receive; errno = Connection refused的错误,连接被拒绝了,到底是什么原因呢? 这 ...
- android中使用MediaPlayer和SurfaceView播放视频
package com.test.video; import java.io.IOException; import android.media.AudioManager; import androi ...
- InnoDB global status
常见参数 Innodb_buffer_pool_pages_free 发现 Innodb_buffer_pool_pages_free 为0 ,则说明buffer_pool 已经被用光,需要增大 in ...
- MySql为某个表增加rownumber
在mySql中,假设表名为tblA,则 select @x:=@x+1 as rownumber, a.* from (select @x:=0) b, tblA a 此语句执行后,则每行数据之前都会 ...