特殊棧(返回棧中最小值)時間複雜度o(1)

題目要求

實現一個特殊的棧,實現棧的基本功能基礎上,在實現返回棧中最小元素的操作,並且複雜度爲o(1)

實現思路

準備一個棧和一個輔助棧,壓入第一個元素時同時向棧和輔助棧壓入第一個元素,
之後每壓一次棧都將該元素和輔助棧對應上一個元素比較,壓棧元素大於輔助棧對應前一個元素,
就再次壓入上一個元素到輔助棧,否則向輔助棧壓入當前元素
這樣輔助棧頂就是我們需要的最小元素

舉個例子:  4 5 3 9 1 0
棧:        輔助棧
壓入4       壓入4
壓入5       壓入4(5和輔助棧上一個元素比較 5>4 壓入輔助棧上一個元素)
壓入3       壓入3(3和輔助棧上一個元素比較 3<4 壓入待壓棧元素)
壓入9       壓入3(9和輔助棧上一個元素比較 9>3 壓入輔助棧上一個元素)
壓入1       壓入1(1和輔助棧上一個元素比較 1<3 壓入待壓棧元素)
輸出輔助棧最後一個元素1爲最小值

代碼

/**
 * @description:實現一個特殊的棧,實現棧的基本功能基礎上,在實現返回棧中最小元素的操作,並且複雜度爲o(1)
 * @Author MRyan
 * @Date 2020/5/16 17:09
 * @Version 1.0
 */
public class StackGetmin {
    //棧
    private static int[] nums;
    //輔助棧
    private static int[] help;
    private static int size;
    private static int helpsize;

    public static void main(String[] args) {
        //初始化
        init(6);
        //壓棧
        push(4);
        push(5);
        push(3);
        push(9);
        push(2);
        //輸出當前棧中最小值 2
        System.out.println("the min is:" + getmin());
        //彈出棧頂元素
        poll();
        //壓棧
        push(1);
        //輸出當前棧中最小值 1
        System.out.println("the min is:" + getmin());
        //當前棧的狀態
        for (int i : nums) {
            System.out.print(i + " ");
        }
        System.out.println();
        //輔助棧的狀態
        for (int i : help) {
            System.out.print(i + " ");
        }


    }

    /**
     * 獲取最小值
     *
     * @return
     */
    public static int getmin() {
        //獲取輔助棧棧頂元素即是最小值
        return help[helpsize];

    }

    /**
     * 初始化
     *
     * @param stacksize
     */
    public static void init(int stacksize) {
        if (stacksize < 0) {
            throw new IllegalArgumentException("the init size is less than 0");
        }
        nums = new int[stacksize];
        help = new int[stacksize];
        size = 0;
        helpsize = 0;
    }

    /**
     * 壓棧
     *
     * @param num
     */
    public static void push(int num) {
        if (size > nums.length) {
            throw new IndexOutOfBoundsException("the stack size is full");
        }
        //壓入第一個元素時 直接將第一個元素壓棧並且壓入輔助棧
        if (size == 0) {
            nums[0] = num;
            help[0] = num;
            //棧下標變爲1 指向第二個元素
            size = 1;
        } else {
            //壓棧
            nums[size++] = num;
            //如果壓入值大於  就再次壓入上一個元素到輔助棧
            if (num > help[helpsize]) {
                help[++helpsize] = help[helpsize - 1];
            }
            //否則壓入待壓棧元素到輔助棧
            else {
                help[++helpsize] = num;
            }
        }
    }

    /**
     * 獲取棧頂元素
     *
     * @return
     */
    public static int peek() {
        if (size <= 0) {
            throw new IndexOutOfBoundsException("the stack size is empty");
        }
        return nums[size - 1];
    }

    /**
     * 彈出棧頂元素
     *
     * @return
     */
    public static int poll() {
        if (size <= 0) {
            throw new IndexOutOfBoundsException("the stack size is empty");
        }
        //彈出棧頂元素時 輔助棧也跟着彈出
        helpsize--;
        return nums[--size];

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章