Min Stack leetcode
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.
使用一个栈,和一个保存最小值的变量min
栈中保存的元素是 新加数和当前min的差,然后更新min,使min保持最小
利用差值使每个栈元素可以包含两个信息量
class MinStack {
public:
void push(int x) {
if (sta.empty())
{
sta.push();
min = x;
}
else
{
sta.push(x - min);
if (x < min)
min = x;
}
} void pop() {
if (sta.empty())
return;
long top = sta.top();
if (top < )
min = min - top;
sta.pop();
} int top() {
if (sta.empty())
return ;
long top = sta.top();
if (top < )
return min;
else
return min + top;
} int getMin() {
return min;
}
private:
stack<long> sta;
long min;
};
Min Stack leetcode的更多相关文章
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded
class MinStack { public: void push(int x) { if(values.empty()) { values.push_back(x); min_indices.pu ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
随机推荐
- HMX-Server C++ 分步式服务器大版本更新了(有源码)
原文地址:http://www.cnblogs.com/hellohuang/p/6294763.html # HMX-ServerHMX-Server分步式服务器框架,主要分为网关.登录.世界.场景 ...
- HDU5057(分块)
Argestes and Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- RabbitMQ确认机制问题处理
现象: 手动在后台创建两个消息反馈队列 代码中监听到消息队列后,对消息进行处理并确认,代码为: 运行代码后,消息未从队列扔出去. 原因及解决方案:后台手动创建队列后,在监听消息中又对队列进行声明创建, ...
- Socket-IOS
Socke Socket又称"套接字” 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 应用程序通常通过"套接字"向网络发出请 ...
- 【原创】梵高油画用深度卷积神经网络迭代十万次是什么效果? A neural style of convolutional neural networks
作为一个脱离了低级趣味的码农,春节假期闲来无事,决定做一些有意思的事情打发时间,碰巧看到这篇论文: A neural style of convolutional neural networks,译作 ...
- Hadoop权威指南:通过distcp并行复制
Hadoop权威指南:通过distcp并行复制 distcp是一个分布式复制程序,改程序可以从Hadoop文件系统间复制大量数据,也可以将大量的数据复制到Hadoop中 distcp的典型应用是在两个 ...
- Hadoop权威指南:FSDataInputStream对象
Hadoop权威指南:FSDataInputStream对象 FileSystem对象中的open()方法返回的是FSDataInputStream对象, 而不是标准的java.io类对象,这个类是继 ...
- 启动activity与使用Intent通信机制解析
我们都知道,一个activity启动另一个activity最简单的方式就是使用startActivity方法: public void startActivity (Intent intent) 但是 ...
- C++ 头文件系列 (algorithm)
简介 algorithm头文件是C++的标准算法库,它主要应用在容器上. 因为所有的算法都是通过迭代器进行操作的,所以算法的运算实际上是和具体的数据结构相分离的 ,也就是说,具有低耦合性. 因此,任何 ...
- AKKA学习笔记
AKKA学习笔记总结 01. AKKA 1. 介绍: Akka基于Actor模型,提供了一个用于构建可扩展的(Scalable).弹性的(Resilient).快速响应的(Responsive)应用程 ...