1. 程式人生 > >Java 刷題 -- 棧(棧最小元素/棧構造佇列/遞迴逆序棧/棧排序)(左程雲面試指南)

Java 刷題 -- 棧(棧最小元素/棧構造佇列/遞迴逆序棧/棧排序)(左程雲面試指南)

返回棧中最小元素

package abc;

import java.util.Stack;

/**
 * 實現一個特殊的棧,在實現基本功能的基礎上,返回棧中最小元素的操作
 * pop push getMin時間複雜度都是O(1)
 * 設計的棧型別可以使用現成的棧結構
 */
public class P01GetMinStack {
    public static class MinStack1 {
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public
MinStack1() { this.stackData = new Stack<Integer>(); this.stackMin = new Stack<Integer>(); } public int pop(){ if (this.stackData.isEmpty()) { throw new RuntimeException("Your stack is empty."); } this
.stackMin.pop(); return this.stackData.pop(); } public void push(int i){ if (stackMin.empty()){ this.stackMin.push(i); } else { int tmp = stackMin.peek(); if (tmp < i){ this.stackMin.
push(tmp); } else { this.stackMin.push(i); } } this.stackData.push(i); } public int getMin(){ if (this.stackMin.isEmpty()) { throw new RuntimeException("Your stack is empty."); } return this.stackMin.peek(); } } public static void main(String[] args){ MinStack1 ms = new MinStack1(); ms.push(3); ms.push(2); ms.push(4); ms.push(1); ms.push(5); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); ms.pop(); System.out.println(ms.getMin()); } }

棧實現佇列

package abc;

import java.util.Stack;

/**
 * 編寫一個類,支援佇列的基本操作(add poll peek)
 */
public class P02StackImplQueue {
    public static class TwoStackQueue {
        public Stack<Integer> stackPush;
        public Stack<Integer> stackPop;

        public TwoStackQueue() {
            stackPush = new Stack<Integer>();
            stackPop = new Stack<Integer>();
        }

        public void add(int i) {
            stackPush.push(i);
        }

        public int poll() {
            if (stackPush.empty() && stackPop.empty()){
                throw new RuntimeException("Queue is empty!");
            } else if (stackPop.empty()){
                while (! stackPush.empty()){
                    int tmp = stackPush.pop();
                    stackPop.push(tmp);
                }
            }
            return stackPop.pop();
        }

        public int peek() {
            if (stackPush.empty() && stackPop.empty()){
                throw new RuntimeException("Queue is empty!");
            } else if (stackPop.empty()){
                while (! stackPush.empty()){
                    int tmp = stackPush.pop();
                    stackPop.push(tmp);
                }
            }
            return stackPop.peek();
        }

    }

    public static void main(String[] args) {
        TwoStackQueue sq = new TwoStackQueue();
        sq.add(1);
        sq.add(2);
        sq.add(3);
        System.out.println(sq.peek());
        System.out.println(sq.poll());
        System.out.println(sq.peek());
        System.out.println(sq.poll());
        System.out.println(sq.peek());
        System.out.println(sq.poll());
    }
}

實現棧的逆序,僅用遞迴

package abc;

import java.util.Stack;

/**
 * 實現棧的逆序,僅用遞迴
 */
public class P03ReverseStack {
    public static void main(String[] args){
        Stack<Integer> test = new Stack<Integer>();
        test.push(1);
        test.push(2);
        test.push(3);
        test.push(4);
        test.push(5);
        reverse(test);
        while (!test.isEmpty()) {
            System.out.println(test.pop());
        }
    }

    private static void reverse(Stack<Integer> stack) {
        if (stack.empty()){
            return;
        }
        int tmp = getLast(stack);
        reverse(stack);
        stack.push(tmp);
    }

    private static int getLast(Stack<Integer> stack) {
        int result = stack.pop();
        if (stack.empty()){
            return result;
        } else {
            int last = getLast(stack);
            stack.push(result);
            return last;
        }
    }
}

在這裡插入圖片描述

棧排序

package abc;

import java.util.Stack;



/**
 * 一個棧中元素型別為整型,需要將該棧從頂到底按照從大到小順序排序,只允許申請一個棧
 */
public class P05SortStack {
    public static void main(String[] args){
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(3);
        stack.push(1);
        stack.push(6);
        stack.push(2);
        stack.push(5);
        stack.push(4);
        sortStack(stack);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }

    private static void sortStack(Stack<Integer> stack) {
        if (stack.empty()){
            return;
        }
        Stack<Integer> help = new Stack<Integer>();
        while (!stack.isEmpty()) {
            int cur = stack.pop();      // get top of stack
            while (!help.isEmpty() && help.peek() < cur) {
                stack.push(help.pop()); // push back in stack if top of help > top of stack
            }
            help.push(cur);             // push in help
        }
        while (!help.isEmpty()) {
            stack.push(help.pop());
        }
    }
}