本问题对应的 leetcode 原文链接:剑指 Offer 30. 包含min函数的栈
问题描述
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
示例 :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.min(); --> 返回 -2.
限制:
各函数的调用总次数不超过 20000 次
解题思路
视频讲解直达: 本题视频讲解
代码实现
时间复杂度:O(1)
额外空间复杂度:O(n)
class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
this.stack1 = new Stack();
this.stack2 = new Stack();
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty() || x <= stack2.peek().intValue()){
stack2.push(x);
}
}
public void pop() {
if(!stack1.isEmpty()){
//Integer, 数值 > 127 I
if(stack1.peek().intValue() == stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
Python
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack1 = []
self.stack2 = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stack1.append(x)
if not self.stack2 or x <= self.stack2[-1]:
self.stack2.append(x)
def pop(self):
"""
:rtype: None
"""
if self.stack1:
if self.stack1[-1] == self.stack2[-1]:
self.stack2.pop()
self.stack1.pop()
def top(self):
"""
:rtype: int
"""
if self.stack1:
return self.stack1[-1]
def min(self):
"""
:rtype: int
"""
if self.stack2:
return self.stack2[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.min()
C++
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
stack1.push(x);
if(stack2.empty() || x <= stack2.top()){
stack2.push(x);
}
}
void pop() {
if(!stack1.empty()){
if(stack1.top() == stack2.top()){
stack2.pop();
}
stack1.pop();
}
}
int top() {
if(!stack1.empty()){
return stack1.top();
}
return 0;
}
int min() {
if(!stack2.empty()){
return stack2.top();
}
return 0;
}
private:
stack<int> stack1, stack2;
};
/**
* 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->min();
*/
Go
type MinStack struct {
stack1 []int
stack2 []int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{[]int{}, []int{}}
}
func (this *MinStack) Push(x int) {
this.stack1 = append(this.stack1, x)
if len(this.stack2) == 0 || x <= this.stack2[len(this.stack2)-1] {
this.stack2 = append(this.stack2, x)
}
}
func (this *MinStack) Pop() {
if len(this.stack1) > 0 {
if this.stack1[len(this.stack1)-1] == this.stack2[len(this.stack2)-1] {
this.stack2 = this.stack2[:len(this.stack2)-1]
}
this.stack1 = this.stack1[:len(this.stack1)-1]
}
}
func (this *MinStack) Top() int {
if len(this.stack1) > 0 {
return this.stack1[len(this.stack1)-1]
}
return 0
}
func (this *MinStack) Min() int {
if len(this.stack2) > 0 {
return this.stack2[len(this.stack2)-1]
}
return 0
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.Min();
*/
JS
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.stack1 = [];
this.stack2 = [];
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.stack1.push(x);
if (this.stack2.length === 0 || x <= this.stack2[this.stack2.length-1]) {
this.stack2.push(x);
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if (this.stack1.length > 0) {
if (this.stack1[this.stack1.length-1] === this.stack2[this.stack2.length-1]) {
this.stack2.pop();
}
this.stack1.pop();
}
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack1[this.stack1.length-1];
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.stack2[this.stack2.length-1];
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.min()
*/
评论(2)
说明:高频题,两种方法的思路都要掌握,一种是时间高,空间低,一种是空间低,时间高, 想问问帅地空间低时间高的方法是什么?
直接遍历找最小值咯,只用一个栈