作者:~小明学编程
文章专栏:每日一练——Java
格言:目之所及皆为回忆,心之所想皆为过往
目录
设计循环队列
链接
题目描述
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。
示例:
MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1); // 返回 true
circularQueue.enQueue(2); // 返回 true
circularQueue.enQueue(3); // 返回 true
circularQueue.enQueue(4); // 返回 false,队列已满
circularQueue.Rear(); // 返回 3
circularQueue.isFull(); // 返回 true
circularQueue.deQueue(); // 返回 true
circularQueue.enQueue(4); // 返回 true
circularQueue.Rear(); // 返回 4
提示:
所有的值都在 0 至 1000 的范围内;
操作数将在 1 至 1000 的范围内;
请不要使用内置的队列库。
代码
public class MyCircularQueue {
private int[] elem;
private int rear;
private int front;
public MyCircularQueue(int k) {
this.elem = new int[k+1];
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
} else {
this.elem[this.rear] = value;
this.rear = (this.rear+1)%this.elem.length;
return true;
}
}
public boolean deQueue() {
if (isEmpty()) {
return false;
} else {
this.front = (this.front+1) % this.elem.length;
return true;
}
}
public int Front() {
if (isEmpty()) {
return -1;
} else {
return this.elem[this.front];
}
}
public int Rear() {
if (isEmpty()) {
return -1;
} else {
if (this.rear==0) {
int index = this.elem.length-1;
return this.elem[index];
} else {
return this.elem[this.rear-1];
}
}
}
public boolean isEmpty() {
return this.front==this.rear;
}
public boolean isFull() {
if ((this.rear+1)%elem.length==this.front) {
return true;
} else {
return false;
}
}
}
用队列实现栈
链接
题目描述
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
提示:
1 <= x <= 9
最多调用100 次 push、pop、top 和 empty
每次调用 pop 和 top 都保证栈不为空
代码
class MyStack {
private Queue<Integer> queue1;
private Queue<Integer> queue2;
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
public void push(int x) {
if (!queue2.isEmpty()) {
queue2.offer(x);
} else {
queue1.offer(x);
}
}
public int pop() {
if (!queue2.isEmpty()) {
int len = queue2.size();
for (int i=0;i<len-1;i++) {
queue1.offer(queue2.poll());
}
return queue2.poll();
} else {
int len = queue1.size();
for (int i=0;i<len-1;i++) {
queue2.offer(queue1.poll());
}
return queue1.poll();
}
}
public int top() {
if (!queue2.isEmpty()) {
int len = queue2.size();
for (int i=0;i<len-1;i++) {
queue1.offer(queue2.poll());
}
int ret = queue2.peek();
queue1.offer(queue2.poll());
return ret;
} else {
int len = queue1.size();
for (int i=0;i<len-1;i++) {
System.out.println(queue1.peek());
queue2.offer(queue1.poll());
}
int ret = queue1.peek();
queue2.offer(queue1.poll());
return ret;
}
}
public boolean empty() {
if (queue1.isEmpty()&&queue2.isEmpty()) {
return true;
} else {
return false;
}
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/