实现带有返回栈中最小元素功能的栈结构

实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返
回栈中最小元素的操作
要求:pop、push、getMin操作的时间复杂度都是O(1)

版本1

package com.chanmufeng.codingInterviewGuide;

import java.util.Stack;

public class GetMinStack1 {
    private static Stack<Integer> dataStack = new Stack<>();
    private static Stack<Integer> minStack = new Stack<>();

    public static void push(int newNum) {
        dataStack.push(newNum);
        if (minStack.isEmpty()) {
            minStack.push(newNum);
        } else {
            minStack.push(Math.min(minStack.peek(), newNum));
        }
    }

    public static int pop() {
        if (dataStack.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        minStack.pop();
        return dataStack.pop();
    }

    public static int getMin() {
        if (minStack.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return minStack.peek();
    }

    public int size() {
        return dataStack.size();
    }


    public static void main(String[] args) {
        GetMinStack1 stack = new GetMinStack1();
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(1);
        stack.push(2);
        stack.push(1);

        while (stack.size()>0){
            System.out.println(stack.getMin());
            stack.pop();
        }
    }
}

版本2

package com.chanmufeng.codingInterviewGuide;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 实现含有获取栈内最小元素功能的栈结构
 * 版本二,优化minStack空间复杂度
 */
public class GetMinStack2 {
    private static ArrayList<Integer> dataStack = new ArrayList<>();
    private static Stack<Integer> minStack = new Stack<>();

    public static void push(int newNum) {
        dataStack.add(newNum);
        if (minStack.isEmpty() || getMin() > newNum) {
            minStack.push(dataStack.size() - 1);
        }
    }

    public static int pop() {
        if (dataStack.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        int size = dataStack.size();
        int res = dataStack.remove(--size);

        //如果弹出的元素的索引为minStack的顶部元素,则出栈
        if (minStack.peek() == size) {
            minStack.pop();
        }
        return res;
    }

    public static int getMin() {
        if (minStack.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return dataStack.get(minStack.peek());
    }

    public int size() {
        return dataStack.size();
    }


    public static void main(String[] args) {
        GetMinStack2 stack = new GetMinStack2();
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(1);
        stack.push(2);
        stack.push(1);

        while (stack.size() > 0) {
            System.out.println(stack.getMin());
            stack.pop();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝉沐风的码场

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值