“`java
精髓就是用另一个栈存放最小值
class MinStack {
Stack stackA;
Stack stackB;
/** initialize your data structure here. */
public MinStack() {
stackA = new Stack<Integer>();
stackB = new Stack<Integer>();
}
public void push(int x) {
stackA.push(x);
if (stackB.isEmpty()) {
stackB.push(x);
} else {
if (x <= stackB.peek()) {
stackB.push(x);
}
}
}
public void pop() {
Integer val = stackA.pop();
if (val.equals(stackB.peek())) {
stackB.pop();
}
}
public int top() {
return stackA.peek();
}
public int getMin() {
return stackB.peek();
}
“`java
class MinStack {
Stack stack1;
Stack 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()||x<=stack2.peek().intValue()){
stack2.push(x);
}
}
public void pop() {
if(!stack1.isEmpty()){
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();
*/
注意判空条件和intvalue
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() || x <= stack2.peek().intValue()){
stack2.push(x);
}
}
public void pop() {
if (!stack1.isEmpty()){
if (stack1.peek().intValue() == stack2.peek().intValue()){
stack2.pop();
}
stack1.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
“`java
class MinStack {
private Stack stack;
private Integer min = Integer.MAX_VALUE;
public MinStack() {
stack = new Stack <> ();
}
每次进栈,除了将需要入栈的值放入栈中外,还会把前一最小值入栈
public void push(int x )
还未入栈前的最小值入栈
stack.push(min);
min = x < min ? x : min ;
正常进栈
stack.push(x);
}
每次出栈,需要出栈两个,一个是当前栈的最小值,一个是需要出栈的正常值
public void pop(){
stack.pop();
min = stack.pop();
}
public int pop(){
return stack.peek();
}
public int min() {
return min;
}
class MinStack {
private Stack<Integer> stack;
private Integer min = Integer.MAX_VALUE;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}
/**
每次进栈,除了将需要入栈的值放入栈中外,还会把前一最小值入栈
*/
public void push(int x) {
// 还未入栈前的最小值入栈
stack.push(min);
// 获取栈的最小值
min = x < min ? x : min;
// 正常进栈
stack.push(x);
}
/**
每次出栈,需要出栈两个,一个是当前栈的最小值,一个是需要出栈的正常值
*/
public void pop() {
stack.pop();
min = stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return min;
}
}
/**
* 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;
Stack<Integer> minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
minStack.push(Integer.MAX_VALUE);
}
//优化
public void push(int val) {
stack.push(val);
minStack.push(Math.min(minStack.peek(),val));
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
//不使用额外的空间
class MinStack {
//栈中不仅仅保存数据,还保存最小元素值
Stack<Integer> stack;
//保存当前栈中最小元素
int min;
public MinStack() {
stack = new Stack<>();
min = Integer.MAX_VALUE;
}
//如果入栈元素比min更小,则min先入栈,入栈元素再入栈
public void push(int val) {
if(val <= min){
stack.push(min);
min = val;
}
stack.push(val);
}
//如果出栈元素是min,则下一个出栈元素是min
public void pop() {
int top = stack.pop();
if(top == min){
min = stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
public class MinStack {
private Deque<Integer> mStack;
//辅助栈,记录当前栈的最小值
private Deque<Integer> minStack;
public MinStack() {
mStack = new LinkedList<>();
minStack = new LinkedList<>();
minStack.push(Integer.MAX_VALUE);
}
public void push(int val) {
mStack.push(val);
minStack.push(Math.min(val,minStack.peek()));
}
public void pop() {
mStack.pop();
minStack.pop();
}
public int top() {
return mStack.peek();
}
public int min() {
return minStack.peek();
}
}
class MinStack {
Deque<Integer> stack1,stack2;
/** initialize your data structure here. */
public MinStack() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
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();
}
}
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack();
this.minStack = new Stack();
}
public void push(int x) {
if(minStack.isEmpty()|| minStack.peek().intValue() >= x){
minStack.add(x);
}
stack.add(x);
}
public void pop() {
if(!minStack.isEmpty()){
if(stack.peek().intValue() == minStack.peek().intValue()){
minStack.pop();
}
}
stack.pop();
}
public int top() {
return stack.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 {
Stack<Integer> stack;
Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (minStack.empty() || x <= minStack.peek()) {
minStack.push(x);
}
}
public void pop() {
int temp = stack.pop();
if (minStack.peek() == temp) {
minStack.pop();
}
}
public int top() {
return stack.peek();
}
public int min() {
return minStack.peek();
}
}
class MinStack {
private Deque<Integer> stack, minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new ArrayDeque<>();
minStack = new ArrayDeque<>();
}
public void push(int x) {
stack.push(x);
if (minStack.isEmpty() || x < minStack.peek()) {
minStack.push(x);
} else {
minStack.push(minStack.peek());
}
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return minStack.peek();
}
}
class MinStack {
public:
stack<int> stk;
stack<int> support_stk;
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
stk.push(x);
if(!support_stk.empty() && support_stk.top() < x)
support_stk.push(support_stk.top());
else
support_stk.push(x);
}
void pop() {
stk.pop();
support_stk.pop();
}
int top() {
return stk.top();
}
int min() {
return support_stk.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->min();
*/
所有操作的时间复杂度都是O(1),空间复杂度O(n)
我们可以在每个元素 a 入栈时把当前栈的最小值 m 存储起来。在这之后无论何时,如果栈顶元素是 a,我们就可以直接返回存储的最小值 m
辅助栈中的数是对应位置的结果
也可以不使用辅助栈,保留当前最小值和栈中原数与最小值的差值,并且防止溢出使用long long类型
class MinStack {
//stack2仅存放最小元素
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() || x <= stack2.peek().intValue()) {
stack2.push(x);//更新最小值
}
}
public void pop() {
if(!stack1.isEmpty()) {
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();
*/
public MinStack() {
minStack = new Stack<>();
stack = new Stack<>();
minStack.add(Integer.MAX_VALUE);
}
public void push(int x) {
stack.push(x);
minStack.push(Math.min(x,minStack.peek()));
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return minStack.peek();
}
/** 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();
}
共以下 50 个回答
“`java stackA; stackB;
精髓就是用另一个栈存放最小值
class MinStack {
Stack
Stack
}
“`java stack1; stack2; (); ();
class MinStack {
Stack
Stack
/** initialize your data structure here. */
public MinStack() {
stack1 = new Stack
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();
*/
注意判空条件和intvalue
时间 O(1)
空间 O(n)
class MinStack {
}
时间复杂度O(1)
空间复杂度O(n)
“`java stack;
class MinStack {
private Stack
private Integer min = Integer.MAX_VALUE;
public MinStack() {
stack = new Stack <> ();
}
每次进栈,除了将需要入栈的值放入栈中外,还会把前一最小值入栈
public void push(int x )
还未入栈前的最小值入栈
stack.push(min);
min = x < min ? x : min ; 正常进栈 stack.push(x); } 每次出栈,需要出栈两个,一个是当前栈的最小值,一个是需要出栈的正常值 public void pop(){ stack.pop(); min = stack.pop(); } public int pop(){ return stack.peek(); } public int min() { return min; }
时间复杂度都为:O(1) 空间复杂度:O(n)
通过一个辅助栈来维护最小值
【思路】这个题的思路非常有意思,用两个栈进行实现,一个为正常栈,另一个保存了在栈各个阶段的最小值
【时间复杂度】pop/push/min的复杂度均为O(1)
采用的和讲解一样的代码
代码:
该代码实现了一个最小栈的数据结构。它使用两个栈来实现,一个栈用于存储元素,另一个栈用于存储当前栈中的最小元素。具体实现如下:
–
__init__:初始化数据结构,创建一个空的元素栈和最小栈。–
push:将元素x推入元素栈中,并在最小栈中维护当前最小值。如果最小栈为空或者新元素小于等于最小栈的栈顶元素,则将新元素也推入最小栈中。–
pop:从元素栈中弹出元素,并在最小栈中更新最小值。如果弹出的元素等于最小栈的栈顶元素,则同时从最小栈中弹出栈顶元素。–
top:返回元素栈的栈顶元素。–
min:返回最小栈中的最小值。这样,无论何时需要获取最小值,只需访问最小栈的栈顶元素即可。该数据结构的时间复杂度为O(1)。
时间复杂度O(1)
空间复杂度O(n)
时间复杂度O(1), 空间复杂度O(N)
时间复杂度O(1),空间复杂度O(n)
辅助栈
时间复杂度o(1),空间复杂度o(n)
class MinStack { datastack; minstack;
stack
stack
public:
/** initialize your data structure here. */
MinStack() {
};
时间复杂度O(1),空间复杂度O(N)
时间复杂度:O(1)
额外空间复杂度:O(n)
[TOC]
思路
解题方法
复杂度
空间复杂度: $O(n)$
Code
时间复杂度O(1)
空间复杂度O(N)
“`cpp s1; s2;
//c++实现
class MinStack {
public:
/** initialize your data structure here. */
stack
stack
MinStack() {
};
通过建立辅助栈实现
C++实现 利用辅助栈 每插入一个元素 存储当前状态下的最小值在辅助栈中
C++实现
这个加个放最小数据的栈
”’java 实现 A,B;
class MinStack {
Stack
/** initialize your data structure here. */
public MinStack() {
A=new Stack<>();
B=new Stack<>();
}
}
时间复杂度:O(n)
空间复杂度:O(1)
辅助栈
时间:O(1)
空间:O(n)
使用辅助栈存储最小元素
C++
TC: O(1)
SC: O(n)
定义一个栈,栈的元素包含当前的栈和到目前为止的最小值
时间复杂度:O(1)
空间复杂度:O(n)
1、辅助栈保存当前最小的数据
2、进栈时,s1入栈,判断s2栈顶元素和s1栈顶,是否s1<=s2,是则s2入栈,否则只有s1入栈
3、出栈时,s1正常出栈,s2中栈顶如果与s1相同,则s2也出栈
4、取最小时,取s2栈顶即可
所有操作的时间复杂度都是O(1),空间复杂度O(n)
我们可以在每个元素 a 入栈时把当前栈的最小值 m 存储起来。在这之后无论何时,如果栈顶元素是 a,我们就可以直接返回存储的最小值 m
辅助栈中的数是对应位置的结果
也可以不使用辅助栈,保留当前最小值和栈中原数与最小值的差值,并且防止溢出使用long long类型
思路:使用辅助栈,保存min的信息。
入栈时若有更小元素,则辅助栈也压入更小元素;若无,则继续压入栈顶元素;
出栈时,数据栈与辅助栈同时出栈;
取最小值时,直接取辅助栈顶部元素即可
C++:借助辅助栈,有一个栈存储最小元素 s1,s2;
class MinStack {
public:
/** initialize your data structure here. */
stack
MinStack()
{
};
思路: MyStack; minstack;
初始化两个栈,一个主栈,一个存储某个值入栈时的栈顶的最小元素。
”’C++
class MinStack {
public:
/** initialize your data structure here. */
stack
stack
MinStack() {
};
”’
使用一个辅助栈,来保存当前栈的最小值。
class MinStack { stack1; stack2;
Stack
Stack
}
时间复杂度:O(1)
额外空间复杂度:O(n)
c++只能使用辅助栈,借助整数不行,亲测 minst; st;
stack
stack
MinStack() {
minst.push(INT_MAX);
}
时间复杂度:O(1)
额外空间复杂度:O(n)
java实现
跟着帅地的思路自己敲了一遍
方法一:使用两个栈,一个模拟栈,另一个栈记录各元素插入时模拟栈的最小值
【时间O(1),空间O(n)】
一个模拟栈,另一个记录最小值