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;
}
};
}
}