class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
public void push(int x) {
if (stack2.isEmpty() || stack2.peek().intValue() >= x){
stack2.push(x);
}
stack1.push(x);
}
public void pop() {
int temp = stack1.pop();
if (stack2.peek().intValue()==temp){
stack2.pop();
}
}
public int top() {
return stack1.peek().intValue();
}
public int getMin() {
return stack2.peek().intValue();
}
}
/**
* 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 {
/** initialize your data structure here. */
private Stack<Integer> s1;
private Stack<Integer> s2;
public MinStack() {
s1 = new Stack<Integer>();
s2 = new Stack<Integer>();
}
public void push(int x) {
//s1正常入栈
s1.push(x);
//s2保存最小值,为空时直接压入,否则入栈的值<=s2栈顶值才压入
if(s2.isEmpty() || x <= s2.peek().intValue()){
s2.push(x);
}
}
public void pop() {
if(!s1.isEmpty()){
//s1正常出栈,s1和s2栈顶值相等时,s2才出栈
//s1.peek().equals(s2.peek())
if(s1.peek().intValue() == s2.peek().intValue()){
s2.pop();
}
s1.pop();
}
}
public int top() {
return s1.peek();
}
public int min() {
return s2.peek();
}
}
/**
* 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();
*/
// 时间复杂度均为 O(1) 空间复杂度O(N)
class MinStack {
Stack xStack;
Stack minStack;
/** initialize your data structure here. */
public MinStack() {
xStack = new Stack();
minStack = new Stack();
minStack.push(Integer.MAX_VALUE);
}
public void push(int x) {
xStack.push(x);
minStack.push(Math.min(minStack.peek(),x));
}
public void pop() {
xStack.pop();
minStack.pop();
}
public int top() {
return xStack.peek();
}
public int min() {
return minStack.peek();
}
/**
* 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();
*/
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {}
void push(int x) {
if(stack.empty()){
stack.push_back(make_pair(x,x));
}
else{
int less;
less = stack.back().second;
if(less> x )
less = x;
stack.push_back(make_pair(x, less));
}
}
void pop() { int ret = stack.back().first;
stack.pop_back();
}
int top() { return stack.back().first; }
int min() { return stack.back().second; }
private:
vector<pair<int, int>> stack;
};
/**
* 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();
*/
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()){
//数值>127
if(stack1.peek().intValue() == stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
/**
* 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();
*/
class MinStack {
public static Stack<Integer> s,arr;
/** initialize your data structure here. */
public MinStack() {
s = new Stack<>();
arr = new Stack<>();
arr.push(Integer.MAX_VALUE);
}
public void push(int x) {
s.push(x);
arr.push(Math.min(x,arr.peek()));
}
public void pop() {
s.pop();
arr.pop();
}
public int top() {
return s.peek();
}
public int min() {
return arr.peek();
}
}
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
//更新最小栈
// 如果最小栈为空或新元素 x 小于等于最小栈的栈顶元素,则将 x 压入最小栈
if(minStack.isEmpty() || x <= minStack.peek()) {
minStack.push(x);
}
}
public void pop() {
int x = stack.pop();
//如果弹出的元素恰好是最小站的栈顶元素,则需要同时从最小栈中弹出元素
//peek() 是一个栈操作,它返回最小栈的栈顶元素,而不会将栈中的元素弹出
if(x == minStack.peek()) {
minStack.pop();
}
}
public int top() {
return stack.peek();
}
public int min() {
return minStack.peek();
}
class MinStack {
Deque<Integer> stack;
Deque<Integer> min;
/** initialize your data structure here. */
public MinStack() {
stack = new ArrayDeque<>();
min = new ArrayDeque<>();
}
public void push(int x) {
stack.push(x);
if (!min.isEmpty() && x > min.peek()) {
min.push(min.peek());
} else {
min.push(x);
}
}
public void pop() {
stack.pop();
min.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return min.peek();
}
}
class MinStack {
public Stack<Integer> stack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}
//每次把当前最小的值压入栈顶
public void push(int x) {
if(stack.empty())
{
stack.push(x);
stack.push(x);
}
else
{
int minNum = stack.peek();
minNum = Math.min(minNum, x);
stack.push(x);
stack.push(minNum);
}
}
public void pop() {
stack.pop();
stack.pop();
}
public int top() {
int tmp = stack.peek();
stack.pop();
int top = stack.peek();
stack.push(tmp);
return top;
}
public int min() {
return stack.peek();
}
}
/**
* 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();
*/
class MinStack {
/** initialize your data structure here. */
Stack<Integer> A, B;
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek() >= x){
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek())){
B.pop();
}
}
public int top() {
return A.peek();
}
public int min() {
return B.peek();
}
}
class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty() || stack2.peek()>=x){
stack2.push(x);
}
}
public void pop() {
if(stack1.pop().equals(stack2.peek())){
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
/**
* 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();
*/
//一个栈实现
class MinStack {
Stack<Integer> stack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}
public void push(int x) {
if(stack.isEmpty() || x < stack.peek()){
stack.push(x);
stack.push(x);
}else{
int minVal = stack.peek();
stack.push(x);
stack.push(minVal);
}
}
public void pop() {
if(stack.isEmpty()) return;
stack.pop();
stack.pop();
}
public int top() {
int minVal = stack.pop();
int topVal = stack.peek();
stack.push(minVal);
return topVal;
}
public int min() {
return stack.peek();
}
}
/**
* 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();
*/
class MinStack {
Stack<Integer> stack1;
Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
stack1=new Stack<Integer>();
stack2=new Stack<Integer>();
}
public void push(int x) {
stack1.push(x);
if(stack2.isEmpty()||x<=stack2.peek().intValue()){
stack2.push(x);
}
}
public void pop() {
if(stack1.peek().intValue()==stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
/**
* 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();
*/
class MinStack {
Stack<Integer> A, B;
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.add(x);
if(B.empty() || B.peek() >= x)
B.add(x);
}
public void pop() {
if(A.pop().equals(B.peek()))
B.pop();
}
public int top() {
return A.peek();
}
public int min() {
return B.peek();
}
}
class MinStack {
// 辅助栈解决
/** initialize your data structure here. */
Deque<Integer> xStack;
Deque<Integer> minStack;
public MinStack() {
xStack = new LinkedList<Integer>();
minStack = new LinkedList<Integer>();
minStack.push(Integer.MAX_VALUE);
}
public void push(int x) {
xStack.push(x);
minStack.push(Math.min(minStack.peek(), x));
}
public void pop() {
xStack.pop();
minStack.pop();
}
public int top() {
return xStack.peek();
}
public int min() {
return minStack.peek();
}
}
五
class MinStack {
Stack stack1;
Stack 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()) {
if (stack1.pop().intValue() == stack2.peek().intValue()) {
stack2.pop();
}
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
/**
* 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();
*/
共以下 44 个回答
辅助栈思路
使用辅助栈,保存非严格降序的元素,返回最小元素时,即返回辅助栈的栈顶元素。时间复杂度O(1),空间复杂度O(n)
// 时间复杂度均为 O(1) 空间复杂度O(N) xStack; minStack; (); ();
class MinStack {
Stack
Stack
/** initialize your data structure here. */
public MinStack() {
xStack = new Stack
minStack = new Stack
minStack.push(Integer.MAX_VALUE);
}
//
时间复杂度O(1)
空间复杂度O(n)
测试通过
/* x_stack; min_stack;
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
*/
class MinStack {
stack
stack
public:
MinStack() {
min_stack.push(INT_MAX);
}
};
/**
* 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();
*/
使用辅助stack
栈中记录当前值与最小值的差值,时间空间均为O(1).
时间复杂度O(1),空间复杂度O(2n)
//C++实现
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
private: stack1, stack2;
stack
};
思路:本题最妙的就是使用一个同样的栈来记录在当前栈的情况下,栈中最小元素是谁,刚开始使用的是单调栈来记录最小值,时间复杂度为O(n)超时
利用辅助栈存当前位置的最小值
时间复杂度:O(1)
空间复杂度:O(n)
”’java stack; minStack;
class MinStack {
//存储栈中的元素
private Stack
//存储当前栈中的最小元素
private Stack
}
”’
用两个栈来完成功能,第一个栈就是正常存储,第二个栈通过判断栈顶元素和新元素的大小来添加较小的元素
不使用辅助栈的话,用一个int变量来记录最小值,然后stack用来记录元素与当前最小元素的差值。
在leetcode上的原题需要把int 改为long long,否则会溢出
class MinStack { stack; minstack; (); ();
Stack
Stack
//普通栈的同时定义一个辅助最小栈
/** initialize your data structure here. */
public MinStack() {
stack =new Stack
minstack =new Stack
}
}
辅助栈做法
包含min函数的栈
两种方法: 辅助栈 & 单栈差值存储
辅助栈
一个栈正常存储数据,另一个栈用来维护最小值,存入比较过程中元素较小的一个
差值存储(单栈)
这种方法比较不好理解。
所谓存差值,就是data = x - min;data就是差值
push:刚开始栈为空,存入最小值;栈不为空,存入差值。
pop:判断栈顶元素是否大于0,再次强调,栈中存的是差值,不是本身。我们通过判断栈顶元素是否大于0可以知道,push进来的那个数是不是当前栈中的最小值,如果大于0,说明 x > min,那存入的就不是最小值,我们直接弹出差值。
如果小于0,说明 x < min,说明压进来的值比当前min值还小,那它应该才是最小值,它依旧是要被弹出的,此时min = 当前这个值。但在此之前,你需要更新这个最小值。为什么?因为它是当前栈的最小值,弹出之后,谁会拿到这个最小的称号?这个问题需要考虑。简单运算:data = x - min; min' = x' - data; 前后的 x' = min;
top:取栈顶,也是分大于0和小于0;大于0,不是最小值,返回 栈顶元素+min;小于0,是最小值,直接返回min
min:直接返回min就好了。
最小函数栈
Day5
//Java 代码中,由于 Stack 中存储的是 int 的包装类 Integer ,因此需要使用 equals() 代替 来比较值是否相等。此题如果用将会无法通过 Integer的equals重写过,比较的是内部value的值, 如果在[-128,127]会被cache缓存,超过这个范围则比较的是对象是否相同。
“`c++ s; Mins;
//时间复杂度:O(1)
//空间复杂度:O(N)
class MinStack {
public:
stack
stack
/** initialize your data structure here. */
MinStack() {
if(s.empty())
{
s.push(x);
Mins.push(x);
return;
}
int prevMin = Mins.top();
int curMin = x < prevMin ? x : prevMin;
s.push(x);
Mins.push(curMin);
}
s.pop();
Mins.pop();
}
return s.top();
}
return Mins.top();
}
};
用列表模拟栈
class MinStack:
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++代码 维护一个栈顶为最小值的最小栈 instack,minstack;//最小栈维护栈顶为最小值
class MinStack {
stack
public:
/** initialize your data structure here. */
MinStack() {
};
代码如下
两个栈
时间复杂度:O(1)
空间复杂度:O(n)
通过额外的一个辅助栈解决问题
五 stack1; stack2;
class MinStack {
Stack
Stack
/** initialize your data structure here. */
public MinStack() {
this.stack1 = new Stack();
this.stack2 = new Stack();
}
}
/**
* 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();
*/
辅助栈法
时间复杂度:O(1)
空间复杂度:O(N)
时间:O(1)
空间: O(n)