剑指offer面试题30(java版):包含min函数的栈

welcome to my blog

剑指offer面试题30(java版):包含min函数的栈

题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))

第四次做; 核心: 如何维护最小值? stack2的栈顶元素是当前的最小值; 如果x比stack2栈顶元素小,就压入x; 如果x比stack2栈顶元素大,就压入stack2的栈顶元素
class MinStack {
    private int min;
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    /** initialize your data structure here. */
    public MinStack() {
        min  = Integer.MAX_VALUE;
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
        //这么写是错误的, 因为如果stack2空了, 此时的min是历史的min, 而此时应该重置min
        // if(x<min)
        //     min = x;
        // stack2.push(min);
        
        
        //这样写也不对, 如果stack2不为空, 但是min被弹出了, 此时的min并没有改变, 而正确的min应该是stack2栈顶元素!
        // if(stack2.isEmpty())
        //     min = x;
        // if(x < min)
        //     min = x;
        // stack2.push(min);
        
        if(stack2.isEmpty()){
            stack2.push(x);
        }
        else{
            stack2.push(x<stack2.peek()? x:stack2.peek());
        }
    }
    
    public void pop() {
        stack1.pop();
        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();
 */

笔记

  • 声明并初始化栈 Stack s = new Stack(); 一般都要加上泛型, 要不默认元素类型为Object
  • s.pedk():查看栈顶元素但并不弹出

思路

  • 创建一个辅助栈, 只存放主栈中的最小值, 举个例子
  • 主栈: 3,4,5,5,5,6,1,2,7
  • 辅助栈:3,3,3,3,3,3,1,1,1
第三次做, s2并不是每次都push, 减少了push操作的流程, 但是会增加pop操作的流程
import java.util.Stack;

public class Solution {

    Stack<Integer> s1 = new Stack<Integer>();
    Stack<Integer> s2 = new Stack<Integer>();
    int min = Integer.MAX_VALUE;
    public void push(int node) {
        s1.push(node);
        if(node<min){
            s2.push(node);
            min = node;
        }
    }
    
    public void pop() {
        if(s1.pop()==s2.peek())
            s2.pop();
    }
    
    public int top() {
        return s1.peek();
    }
    
    public int min() {
        return s2.peek();
    }
}
第二次做, 好好使用辅助栈s2
import java.util.Stack;

public class Solution {
    /*
    使用两个栈实现
    s1正常压入
    s2为空时直接压入
    s2中有元素时,取出栈顶元素,如果待压入的元素小于栈顶元素则压入该元素;如果待压入的元素大于等于栈顶元素则在压入一次栈顶元素
    
    改写一下常规的push和pop即可
    */
    Stack<Integer> s1 = new Stack<>();
    Stack<Integer> s2 = new Stack<>();
    public void push(int node) {
        s1.push(node);
        if(s2.isEmpty())
            s2.push(node);
        else{
            int temp = node < s2.peek()? node:s2.peek();
            s2.push(temp);
        }
    }
    
    public void pop() {
        s1.pop();
        s2.pop();
    }
    
    public int top() {
        return s1.peek();
    }

    public int min() {
        return s2.peek();
    }
}
第二次做, 可以拿个全局变量记录最小值, 不用每次都访问s2的栈顶
import java.util.Stack;

public class Solution {
    /*
    使用两个栈实现
    s1正常压入
    s2为空时直接压入
    s2中有元素时,取出栈顶元素,如果待压入的元素小于栈顶元素则压入该元素;如果待压入的元素大于等于栈顶元素则在压入一次栈顶元素
    
    改写一下常规的push和pop即可
    */
    Stack<Integer> s1 = new Stack<>();
    Stack<Integer> s2 = new Stack<>();
    int min = Integer.MAX_VALUE;
    public void push(int node) {
        s1.push(node);
        if(node < min)
            min = node;
        s2.push(min);
    }
    
    public void pop() {
        s1.pop();
        s2.pop();
    }
    
    public int top() {
        return s1.peek();
    }

    public int min() {
        return s2.peek();
    }
}
import java.util.Stack;

public class Solution {

    Stack<Integer> s = new Stack<Integer>();
    Stack<Integer> assistS = new Stack<Integer>();
    int minV = Integer.MAX_VALUE;
    public void push(int node) {
        s.push(node);
        if(minV > node)
            minV = node;
        assistS.push(minV);
    }
    
    public void pop() {
        s.pop();
        assistS.pop();
    }
    
    public int top() {
        return s.pop();
    }
    
    public int min() {
        return assistS.peek();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值