题目
描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的 min 函数,输入操作时保证 pop、top 和 min 函数操作时,栈中一定有元素。
此栈包含的方法有:
push(value):将value压入栈中
pop():弹出栈顶元素
top():获取栈顶元素
min():获取栈中最小元素
数据范围:操作数量满足 ,输入的元素满足
进阶:栈的各个操作的时间复杂度是 ,空间复杂度是
示例:
输入: [“PSH-1”,“PSH2”,“MIN”,“TOP”,“POP”,“PSH1”,“TOP”,“MIN”]
输出: -1,2,1,-1
解析:
"PSH-1"表示将-1压入栈中,栈中元素为-1
"PSH2"表示将2压入栈中,栈中元素为2,-1
“MIN”表示获取此时栈中最小元素==>返回-1
"TOP"表示获取栈顶元素==>返回2
"POP"表示弹出栈顶元素,弹出2,栈中元素为-1
"PSH1"表示将1压入栈中,栈中元素为1,-1
"TOP"表示获取栈顶元素==>返回1
“MIN”表示获取此时栈中最小元素==>返回-1
思路
用一个list模拟栈的数据结构,用函数min返回list的最小值。也可以用另外一个list或者栈对应的在每一次pop或者append的时候记录最小值。
代码
python版本:
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.storage=[]
def push(self, node):
self.storage.append(node)
# write code here
def pop(self):
# write code here
self.storage.pop()
def top(self):
# write code here
return self.storage[-1]
def min(self):
# write code here
return min(self.storage)
c++版本:
class Solution {
public:
vector<int> storage;
void push(int value) {
storage.push_back(value);
}
void pop() {
storage.pop_back();
}
int top() {
return storage[storage.size()-1];
}
int min() {
return *min_element(storage.begin(), storage.end());
}
};