构造一个栈,pop(),push(),min(),max()复杂度都是o(1)的Java实现

思路:

1、利用额外空间处理

2、push时,把最小元素压入栈中,再有新元素进入等于当前最小元素则重复压入,保证压入重复元素是无误

3、poll时,弹出元素,与当前最小值一致则弹出。

代码如下:

package com.my.test.datastructure.stack;

import java.util.Stack;

/*
 * 实现一个栈 获取最大、最小值、pop()、push()都是O(1)复杂度
 */
public class MinMaxStack
{
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();
    private Stack<Integer> maxStack = new Stack<>();
    
    public void push(int value) {
        // 插入元素
        stack.push(value);
        
        // 第一次push元素直接插入 或者 小于等于栈顶元素,则插入最小栈
        if (minStack.empty() || minStack.peek() >= value) {
            minStack.push(value);
        }
        
        // 第一次push元素 或者 大于等于栈顶元素,则插入最大栈
        if (maxStack.empty() || maxStack.peek() <= value) {
            maxStack.push(value);
        }
    }
    
    public int pop() {
        if (stack.empty()) {
            System.out.println("stack is empty");
            throw new RuntimeException("stack is empty");
        }
        int popValue = stack.pop();
        // 元素为最大值或最小值则弹出
        if (popValue == minStack.peek()) {
            minStack.pop();
        }
        if (popValue == maxStack.peek()) {
            maxStack.pop();
        }
        return popValue;
    }
    
    public int getMin() {
        if (minStack.empty()) {
            throw new RuntimeException("min stack is empty");
        }
        return minStack.peek();
    }
    
    public int getMax() {
        if (maxStack.empty()) {
            throw new RuntimeException("max stack is empty");
        }
        return maxStack.peek();
    }
    
    public static void main(String args[]) {
        MinMaxStack stack = new MinMaxStack();
        stack.push(2);
        stack.push(3);
        stack.push(1);
        stack.push(3);
        stack.push(1);
        stack.push(4);
        stack.push(2);
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
    }

}

 

一种更加简单的方案:

1、每次push元素,如果比当前最小元素小,则在最小栈中加入当前栈顶元素

2、每次pop()时,直接pop即可

代码如下:

package com.my.test.datastructure.stack;

import java.util.Stack;

/*
 * 实现一个栈 获取最大、最小值、pop()、push()都是O(1)复杂度
 */
public class MinMaxStack2
{
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();
    private Stack<Integer> maxStack = new Stack<>();
    
    public void push(int value) {
        // 插入元素
        stack.push(value);
        
        // 第一次push元素直接插入 或 小于等于栈顶元素,则插入最小栈
        if (minStack.empty() || minStack.peek() >= value) {
            minStack.push(value);
        }
        // 否则再次插入当前栈顶元素
        else {
            minStack.push(minStack.peek());
        }
        
        // 第一次push元素 或 大于等于栈顶元素,则插入最大栈
        if (maxStack.empty() || maxStack.peek() <= value) {
            maxStack.push(value);
        }
        // 否则再次插入当前栈顶元素
        else {
            maxStack.push(maxStack.peek());
        }
    }
    
    public int pop() {
        if (stack.empty()) {
            System.out.println("stack is empty");
            throw new RuntimeException("stack is empty");
        }
        // 最小栈 最大栈数据直接弹出
        minStack.pop();
        maxStack.pop();
        
        return stack.pop();
    }
    
    public int getMin() {
        if (minStack.empty()) {
            throw new RuntimeException("min stack is empty");
        }
        return minStack.peek();
    }
    
    public int getMax() {
        if (maxStack.empty()) {
            throw new RuntimeException("max stack is empty");
        }
        return maxStack.peek();
    }
    
    public static void main(String args[]) {
        MinMaxStack2 stack = new MinMaxStack2();
        stack.push(2);
        stack.push(3);
        stack.push(1);
        stack.push(3);
        stack.push(1);
        stack.push(4);
        stack.push(2);
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
        stack.pop();
        System.out.println(String.format("min: %s, max: %s", stack.getMin(), stack.getMax()));
    }

}

 

参考:

栈和队列实现:https://www.cnblogs.com/qianguyihao/p/4795984.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值