LeetCode 225: 用队列实现栈

题目:
在这里插入图片描述
在这里插入图片描述
题解:
在这里插入图片描述

代码示例:

package com.zy.leetcode.LeetCode_225;

import com.zy.queue_code.demo04.ArrayQueue3;

/**
 * @Author: zy
 * @Date: 2025-01-10-10:26
 * @Description:
 */
public class LeetCode_225 {

    ArrayQueue3<Integer> queue = new ArrayQueue3<>(100);

    private int size = 0;

    public void push(int x) {
        queue.offer(x);
        for (int i = 0; i < size; i++) {
            queue.offer(queue.poll());
        }
        size++;

    }

    public int pop() {
        size--;
        return queue.poll();

    }

    public int top() {
        return queue.peek();

    }

    public boolean empty() {
        return size == 0;
    }

    public static void main(String[] args) {

        LeetCode_225 leetCode_225 = new LeetCode_225();
        leetCode_225.push(1);
        leetCode_225.push(2);
        System.out.println(leetCode_225.top()); // 2

        System.out.println(leetCode_225.pop()); // 2
        System.out.println(leetCode_225.empty()); // false
        leetCode_225.push(3);
        System.out.println(leetCode_225.top()); // 3

        System.out.println(leetCode_225.pop()); // 3

        System.out.println(leetCode_225.empty()); // false

    }
}

package com.zy.queue_code.demo04;

import com.zy.queue_code.demo01.Queue;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @Author: zy
 * @Date: 2025-01-07-12:23
 * @Description: 求模运算:
 * - 如果除数是2的n次方
 * - 那么被除数的后n 位即为余数(模)
 * 求被除数的后n 位方法:与 2^n-1 按位与
 */
public class ArrayQueue3<E> implements Queue<E>, Iterable<E> {

    private final E[] array;

    public int head = 0;

    public int tail = 0;

    /**
     * 如果除数是2的n次方
     *
     * // 求离C最近,比C大的 2^n(方法2)
     * C-=1;
     * c |=c>>1;
     * c | =c>>2;
     * c | =c>>4;
     * с | =с>>8;
     * с | =с>>16;
     * C+=1;
     * System.out.println(c);
     * I
     *
     * @param capacity
     */
    public ArrayQueue3(int capacity) {
        //1.抛异常
        //        int num = capacity & (capacity - 1);
        //        if (num != 0) {
        //            throw new IllegalArgumentException("capacity must be a power of 2");
        //        }
        //2.将传入的数据改成一个接近2^n 的数字
        int n = (int) (Math.log(capacity - 1) / Math.log(2)) + 1;
        /**
         * 1     2^0
         * 10    2^1
         * 100   2^2
         * 1000  2^3
         * 10000 2^4
         *
         * 左移
         */
        int capacity2 = 1 << n;
        //3. 创建一个接近2^n 的数组
        array = (E[]) new Object[capacity2];
    }

    @Override
    public boolean offer(E value) {
        //判断是否已经满了?
        if (isFull()) {
            return false;
        }
        //array[(int) (Integer.toUnsignedLong(tail) % array.length)] = value;
        array[tail & (array.length - 1)] = value;
        tail++;
        return true;
    }

    @Override
    public E poll() {
        if (isEmpty()) {
            return null;
        }
        //        E value = array[(int) (Integer.toUnsignedLong(head) % array.length)];
        E value = array[head & (array.length - 1)];
        head++;
        return value;
    }

    @Override
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return array[((head) & (array.length - 1))];
    }

    @Override
    public boolean isEmpty() {
        return head == tail;
    }

    @Override
    public boolean isFull() {
        //return (tail + 1) % array.length == head;
        //tail -array.length == head
        //return (tail - head + array.length) % array.length == 0;
        return tail - array.length == head;
    }

    @NotNull
    @Override
    public Iterator<E> iterator() {
        return new Iterator<>() {
            int p = head;

            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public E next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                E value = array[p & (array.length - 1)];
                //p = (p + 1) % array.length;
                p++;
                return value;
            }
        };
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EdwardYange

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值