首页 > 试题广场 >

最小栈

[编程题]最小栈
  • 热度指数:5595 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解
实现一个最小栈,有三种操作,min:得到栈中的最小值,push:在栈顶插入一个元素,pop:弹出栈顶元素,使这三种操作的时间复杂度都是O(1)
要求:语言不限

输入描述:
第一行是一个数Q,接下来Q行每行表示一个操作,每行首先是操作op
若op==0,则输出当前栈中的最小值;
若op==1,表示push,接着正整数x,把在x放进栈顶;
若op==2,表示pop,弹出栈顶元素
保证Q<=500000,保证op==0或2时(即min操作和pop操作时)栈不为空。
你可以假设一开始栈的空的。


输出描述:
对应每个op==0或2,如果是op==0输出当前栈中的最小值,如果是op==2输出弹出的元素。
示例1

输入

7
1 3
1 4
0
1 2
0
2
0

输出

3
2
2
3

说明

第一个操作为push 3,此时栈元素为3
第二个操作为push 4,此时栈元素为3,4
第三个操作为min,此时栈元素为3,4,输出最小值3
第四个操作为push 2,此时栈元素为3,4,2
第五个操作为min,此时栈元素为3,4,2,输出最小值2
第六个操作为pop,弹出元素2,此时栈元素为3,4,输出弹出的元素2
第七个操作为min,此时栈元素为3,4,输出最小值3
import java.util.Scanner;
import java.util.Stack;


class MinStack{
    // 最小栈,存入栈中的元素的一个数组,长度为2,一个是入栈的元素,另一个是之前入栈内的最小元素
    //数组栈, [当前值, 当前最小值]
    private Stack<int[]> stack = new Stack<>();
    // 构造方法
    public MinStack(){};

    // push方法
    public void push(int val){
        if(stack.isEmpty()){
            stack.push(new int[]{val, val});
        }else{
            stack.push(new int[]{val, Math.min(val, stack.peek()[1])});
        }
    }

    // pop方法
    public int pop(){
        return stack.pop()[0];
    }

    // getMin方法
    public int getMin(){
        return stack.peek()[1];
    }

}
// https://www.nowcoder.com/practice/a4d17eb2e9884359839f8ec559043761
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q, op;
        Q = in.nextInt();
        MinStack minStack = new MinStack();
        for(int i = 0; i < Q; i++){
            op = in.nextInt();
            if(op == 0){
                System.out.println(minStack.getMin());
            }else if(op == 1){
                minStack.push(in.nextInt());
            }else if(op == 2){
                System.out.println(minStack.pop());
            }
        }
    }
}

发表于 2022-02-17 01:09:20 回复(0)
/*40%,在哪里会数组越界,求解答*/

import java.util.*;
class minStack{
    public Stack<Integer> stack = new Stack<Integer>();
    public Stack<Integer> min = new Stack<Integer>();
    public int minval = 0;
    public int pop(){
        int k = 0;
        if(!stack.isEmpty()){
            k=stack.pop();
            min.pop();
            minval = min.peek();
            return k;
        }
        else{
            return -1;
        }
    }
    public void push(int val){
        if(stack.isEmpty()){
            minval = val;
        }else{
            if(val<minval){
                minval = val;
            }
        }
        stack.push(val);
        min.push(minval);
    }
    public int getMin(){
        if(!stack.isEmpty()){
            return min.peek();
        }else{
            return -1;
        }
    }
}
public class Main{
    public static void main(String[] args){
        minStack mmm = new minStack();
        Scanner sc = new Scanner(System.in);
        int Q= sc.nextInt();
        int k = 0;
        int m = 0;
        for(int i = 0;i<Q;i++){
            sc.nextLine();
            k = sc.nextInt();
            if(k == 0){
                System.out.println(mmm.getMin());
            }
            if(k == 1){
                m = sc.nextInt();
                mmm.push(m);
            }
            if(k == 2){
                System.out.println(mmm.pop());
            }
        }
    }
}

发表于 2020-04-02 23:00:10 回复(1)
import java.util.*;

public class Main{
static Stack<Integer> minStack = new Stack<Integer>();
static Stack <Integer> stack = new Stack<Integer>();
public static void main(String [] args){
minStack.push(Integer.MAX_VALUE);
Scanner input = new Scanner(System.in);
int Q = input.nextInt();
while(Q-- > 0){
int op = input.nextInt();
if(op == 0){
System.out.println(getMinStackValue());
}else if(op == 1){
Integer pushValue = input.nextInt();
compareMinStack(pushValue);
stack.push(pushValue);
}else{
minStack.pop();
System.out.println(stack.pop());
}

}
}

public static Integer getMinStackValue(){
return minStack.lastElement();
}

public static void compareMinStack(Integer value){
Integer min = getMinStackValue();
if(value < min){
minStack.push(value);
}else{
minStack.push(min);
}
}
}

emmm运行超时了,通过率只有66.67%,看看大佬们有没有什么优化建议。
编辑于 2020-03-01 14:20:15 回复(1)