【leetcode】155. Min Stack
2016-07-07 23:16
288 查看
一、题目描述
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.
Example:
题目解读:写一个栈要有以下功能:push(),pop(),top(),getMin()能取得当前栈中的最小元素
思路:直接用一个栈就可以实现所有的push(),pop(),top()这三个功能,在设置一个栈用来存每加入一个元素时当前的最小元素。因此在每pop一个元素对于min栈也要pop一个元素。
c++代码(52ms,74.75%)
class MinStack {
public:
/** initialize your data structure here. */
stack<int> min;
stack<int> result;
void push(int x) {
result.push(x);
if(min.empty())
min.push(x);
else{
if(x<min.top())
min.push(x);
else{
min.push(min.top());
}//else
}//else
}//void
void pop() {
result.pop();
min.pop();
}
int top() {
return result.top();
}
int getMin() {
return min.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();
*/
其他代码:
class MinStack {
private:
stack<int> s1;
stack<int> s2;
public:
void push(int x) {
s1.push(x);
if (s2.empty() || x <= getMin()) s2.push(x);
}
void pop() {
if (s1.top() == getMin()) s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
};
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.
Example:
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2.
题目解读:写一个栈要有以下功能:push(),pop(),top(),getMin()能取得当前栈中的最小元素
思路:直接用一个栈就可以实现所有的push(),pop(),top()这三个功能,在设置一个栈用来存每加入一个元素时当前的最小元素。因此在每pop一个元素对于min栈也要pop一个元素。
c++代码(52ms,74.75%)
class MinStack {
public:
/** initialize your data structure here. */
stack<int> min;
stack<int> result;
void push(int x) {
result.push(x);
if(min.empty())
min.push(x);
else{
if(x<min.top())
min.push(x);
else{
min.push(min.top());
}//else
}//else
}//void
void pop() {
result.pop();
min.pop();
}
int top() {
return result.top();
}
int getMin() {
return min.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();
*/
其他代码:
class MinStack {
private:
stack<int> s1;
stack<int> s2;
public:
void push(int x) {
s1.push(x);
if (s2.empty() || x <= getMin()) s2.push(x);
}
void pop() {
if (s1.top() == getMin()) s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
};
相关文章推荐
- interactive_timeout和wait_timeout(
- 我们说的oc是动态运行时语言是什么意思?
- 自定义View注意点
- javascript继承之一
- Java 四种线程池的用法分析
- mybatis入门案例
- ConfigParser模块
- git常用命令总结
- Android之Button
- Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
- 表结构设计
- 二分查找算法
- hibernate入门案例
- Java对象反序列化防护
- Android开发之自定义无背景Dialog的实现
- bzoj3224 Tyvj1728 普通平衡树
- 7月7日实习报告
- day2总结--JS切换卡效果(转自个人印象笔记)
- python3关于urllib中urlopen报错问题的解决
- TimesTen 数据库复制学习:19. 解决复制冲突