【剑指 Offer 30】包含min函数的栈

包含min函数的栈

题目

定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.min();   --> 返回 -2.
提示:

各函数的调用总次数不超过 20000 次
注意:本题与主站 155 题相同:https://leetcode-cn.com/problems/min-stack/

Related Topics
栈
设计

👍 500
👎 0


class MinStack {

    /** initialize your data structure here. */
    public MinStack() {

    }
    
    public void push(int x) {

    }
    
    public void pop() {

    }
    
    public int top() {

    }
    
    public int 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();
 */

思路

  1. 看了解析后,使用双栈
  2. stackA存储所有元素
  3. stackB存储最小值

实现

import java.util.Stack;

//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {

    Stack<Integer> stackA;
    Stack<Integer> stackB;
    /** initialize your data structure here. */
    public MinStack() {
        stackA = new Stack<>();
        stackB = new Stack<>();
    }

    public void push(int x) {
        stackA.push(x);
        if (stackB.empty() || stackB.peek()>=x) stackB.push(x);
    }

    public void pop() {
        int r = stackA.pop();
        if (!stackB.empty() && stackB.peek()==r) stackB.pop();
    }

    public int top() {
        return stackA.peek();
    }

    public int min() {
        return stackB.peek();
    }
}

结果

解答成功:
	执行耗时:14 ms,击败了34.47% 的Java用户
	内存消耗:46 MB,击败了11.51% 的Java用户

链表

class MinStack {
    class ListNode{
        Integer val;
        MinStack.ListNode pre;
        MinStack.ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
    }


    MinStack.ListNode nodeA;
    MinStack.ListNode nodeB;
    /** initialize your data structure here. */
    public MinStack() {
        nodeA = new MinStack.ListNode();
        nodeB = new MinStack.ListNode();
    }

    public void push(int x) {
        MinStack.ListNode node = new MinStack.ListNode(x);
        nodeA.next=node;
        node.pre= nodeA;
        nodeA = node;
        if (nodeB.val==null || nodeB.val>=x) {
            MinStack.ListNode node1 = new MinStack.ListNode(x);
            nodeB.next=node1;
            node1.pre= nodeB;
            nodeB = node1;
        }
    }

    public void pop() {
        MinStack.ListNode node = nodeA;
        if (nodeB.val!=null && nodeB.val.equals(node.val)) {
            nodeB= nodeB.pre;
            nodeB.next=null;
        }
        nodeA=nodeA.pre;
        nodeA.next=null;
    }

    public int top() {
        return nodeA.val;
    }

    public int min() {
        return nodeB.val;
    }
}

结果

解答成功:
	执行耗时:12 ms,击败了99.34% 的Java用户
	内存消耗:45.9 MB,击败了20.39% 的Java用户

数组

class MinStack {

    int[] arrA;
    int[] arrB;
    int lengthA;
    int lengthB;

    /** initialize your data structure here. */
    public MinStack() {
        arrA = new int[10];
        arrB = new int[10];
        lengthA=0;
    }

    public void push(int x) {
        arrA[lengthA]=x;
        lengthA++;
        int[] tempA = autoSize(arrA,lengthA);
        if (tempA!=null){
            arrA=tempA;
        }
        if (lengthB==0||arrB[lengthB-1]>=x){
            arrB[lengthB]=x;
            lengthB++;
            int[] tempB = autoSize(arrB,lengthB);
            if (tempB!=null){
                arrB=tempB;
            }
        }

    }

    private int[] autoSize(int[] arr,int length){
        if (length > arr.length*0.8){
            int[] temp = arr;
            arr = new int[temp.length*2];
            for (int i=0;i<length;i++){
                arr[i]=temp[i];
            }
            return arr;
        }
        return null;
    }

    public void pop() {
        lengthA--;
        if (lengthB!=0 && arrB[lengthB-1]==arrA[lengthA]){
            lengthB--;
        }
    }

    public int top() {
        return arrA[lengthA-1];
    }

    public int min() {
        return arrB[lengthB-1];
    }
}

结果

解答成功:
	执行耗时:15 ms,击败了19.04% 的Java用户
	内存消耗:42.6 MB,击败了99.95% 的Java用户
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值