# 题目地址：

https://leetcode.com/problems/design-front-middle-back-queue/

import java.util.ArrayDeque;
import java.util.Deque;

class FrontMiddleBackQueue {

private Deque<Integer> fDeq, rDeq;
private int size;

public FrontMiddleBackQueue() {
fDeq = new ArrayDeque<>();
rDeq = new ArrayDeque<>();
}

public void pushFront(int val) {
fDeq.offerFirst(val);
size++;
}

public void pushMiddle(int val) {
fDeq.offerLast(val);
size++;
}

public void pushBack(int val) {
rDeq.offerLast(val);
size++;
}

public int popFront() {
if (size == 0) {
return -1;
}

// 注意要判断一下前队列是否空
int res = !fDeq.isEmpty() ? fDeq.pollFirst() : rDeq.pollFirst();
size--;
return res;
}

public int popMiddle() {
if (size == 0) {
return -1;
}

// 如果两个队列size一样，则pop前队列队尾，否则pop后队列队头
int res = fDeq.size() == rDeq.size() ? fDeq.pollLast() : rDeq.pollFirst();
size--;
return res;
}

public int popBack() {
if (size == 0) {
return -1;
}

int res = rDeq.pollLast();
size--;
return res;
}

// 用于调整两个队列的size，保持后半始终不少于前半，并且多不超过1个
if (rDeq.size() - fDeq.size() >= 2) {
fDeq.offerLast(rDeq.pollFirst());
}
if (rDeq.size() < fDeq.size()) {
rDeq.offerFirst(fDeq.pollLast());
}
}
}


03-01 1万+

02-06 1764
02-27 1715
10-21 218
01-04 125
01-14 242
08-16 268
10-06 61
02-15