1. 程式人生 > 其它 >化棧為隊Java版(力扣)

化棧為隊Java版(力扣)

技術標籤:LeetCode佇列javaleetcode

化棧為隊

實現一個MyQueue類,該類用兩個棧來實現一個佇列。

示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

說明:
你只能使用標準的棧操作 – 也就是隻有 push to top, peek/pop from top, size 和 is empty 操作是合法的。
你所使用的語言也許不支援棧。你可以使用 list 或者 deque(雙端佇列)來模擬一個棧,只要是標準的棧操作即可。

假設所有操作都是有效的 (例如,一個空的佇列不會呼叫 pop 或者 peek 操作)。

題意:用兩個棧模擬一個佇列。

思路:棧是先進後出,佇列是先進先出,所以我們可以先用一個棧存著資料,然後用另為一個棧存著順序相反的資料,在進行出隊的操作時,我們從那個存放著相反資料的棧中操作,這樣就模擬了佇列。

程式碼:

class MyQueue {

    //存放數字
    private Stack<Integer> numStack;
    //相反的棧,存放資料的順序相反
    private Stack<Integer> contraryStack;

    /**
     * Initialize your data structure here.
     */
public MyQueue() { // new 出來兩個棧 numStack = new Stack<>(); contraryStack = new Stack<>(); } /** * Push element x to the back of queue. */ public void push(int x) { //往numStack中存放資料 numStack.push(x); } /** * Removes the element from in front of queue and returns that element. */
public int pop() { // 呼叫peek(),對contraryStack存放順序相反的資料 peek(); //彈出 return contraryStack.pop(); } /** * Get the front element. */ public int peek() { //將numStack的資料全部倒出來放到contraryStack中,這樣順序就反過來的,就模擬了佇列 if (contraryStack.isEmpty()) { while (!numStack.isEmpty()) { contraryStack.push(numStack.pop()); } } return contraryStack.peek(); } /** * Returns whether the queue is empty. */ public boolean empty() { return contraryStack.isEmpty() && numStack.isEmpty(); } }

完整程式碼(含測試樣例):

package com.Keafmd.day0104;

import java.util.Stack;

/**
 * Keafmd
 *
 * @ClassName: ImplementQueueusingStacks
 * @Description: 化棧為隊
 * @author: 牛哄哄的柯南
 * @date: 2021-01-04 21:38
 */
public class ImplementQueueusingStacks {
    public static void main(String[] args) {

        MyQueue obj = new MyQueue();
        obj.push(1);
        obj.push(2);
        obj.push(3);
        // peek 不改變棧的值(不刪除棧頂的值),pop會把棧頂的值刪除。
        int param_1 = obj.pop();
        int param_2 = obj.peek();
        boolean param_3 = obj.empty();
        System.out.println(param_1);
        System.out.println(param_2);
        System.out.println(param_3);

    }
}

class MyQueue {

    //存放數字
    private Stack<Integer> numStack;
    //相反的棧,存放資料的順序相反
    private Stack<Integer> contraryStack;

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {
        // new 出來兩個棧
        numStack = new Stack<>();
        contraryStack = new Stack<>();

    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        //往numStack中存放資料
        numStack.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        // 呼叫peek(),對contraryStack存放順序相反的資料
        peek();
        //彈出
        return contraryStack.pop();

    }

    /**
     * Get the front element.
     */
    public int peek() {
        //將numStack的資料全部倒出來放到contraryStack中,這樣順序就反過來的,就模擬了佇列
        if (contraryStack.isEmpty()) {
            while (!numStack.isEmpty()) {
                contraryStack.push(numStack.pop());
            }
        }
        return contraryStack.peek();

    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {

        return contraryStack.isEmpty() && numStack.isEmpty();

    }
}


寫作不易,看完如果對你有幫助,感謝點贊支援!
如果你是電腦端,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

在這裡插入圖片描述
加油!

共同努力!

Keafmd