题目如下:
Design a queue that supports
push
andpop
operations in the front, middle, and back.Implement the
FrontMiddleBack
class:
FrontMiddleBack()
Initializes the queue.void pushFront(int val)
Addsval
to the front of the queue.void pushMiddle(int val)
Addsval
to the middle of the queue.void pushBack(int val)
Addsval
to the back of the queue.int popFront()
Removes the front element of the queue and returns it. If the queue is empty, return-1
.int popMiddle()
Removes the middle element of the queue and returns it. If the queue is empty, return-1
.int popBack()
Removes the back element of the queue and returns it. If the queue is empty, return-1
.Notice that when there are two middle position choices, the operation is performed on the frontmost middle position choice. For example:
- Pushing
6
into the middle of[1, 2, 3, 4, 5]
results in[1, 2, 6, 3, 4, 5]
.- Popping the middle from
[1, 2, 3, 4, 5, 6]
returns3
and results in[1, 2, 4, 5, 6]
.
Example 1:
Input: ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"] [[], [1], [2], [3], [4], [], [], [], [], []] Output: [null, null, null, null, null, 1, 3, 4, 2, -1] Explanation: FrontMiddleBackQueue q = new FrontMiddleBackQueue(); q.pushFront(1); // [1] q.pushBack(2); // [1, 2] q.pushMiddle(3); // [1, 3, 2] q.pushMiddle(4); // [1, 4, 3, 2] q.popFront(); // return 1 -> [4, 3, 2] q.popMiddle(); // return 3 -> [4, 2] q.popMiddle(); // return 4 -> [2] q.popBack(); // return 2 -> [] q.popFront(); // return -1 -> [] (The queue is empty)
Constraints:
1 <= val <= 109
- At most
1000
calls will be made topushFront
,pushMiddle
,pushBack
,popFront
,popMiddle
, andpopBack
.
解题思路:使用两个queue,分别为front和second,始终保证front的长度不小于second,并且与second的长度差不超过2。
代码如下:
class FrontMiddleBackQueue(object): def __init__(self): self.front = [] self.second = [] def pushFront(self, val): """ :type val: int :rtype: None """ self.front.insert(0,val) if len(self.front) - len(self.second) >= 2: self.second.insert(0,self.front.pop(-1)) def pushMiddle(self, val): """ :type val: int :rtype: None """ if len(self.front) == len(self.second): self.front.append(val) else: self.second.insert(0,self.front.pop(-1)) self.front.append(val) def pushBack(self, val): """ :type val: int :rtype: None """ self.second.append(val) if len(self.front) < len(self.second): self.front.append(self.second.pop(0)) def popFront(self): """ :rtype: int """ if len(self.front) == 0: return -1 val = self.front.pop(0) if len(self.front) < len(self.second): self.front.append(self.second.pop(0)) return val def popMiddle(self): """ :rtype: int """ if len(self.front) == 0: return -1 val = self.front.pop(-1) if len(self.front) < len(self.second): self.front.append(self.second.pop(0)) return val def popBack(self): """ :rtype: int """ if len(self.second) == 0 and len(self.front) == 0: return -1 elif len(self.second) == 0 and len(self.front) != 0: return self.front.pop(-1) val = self.second.pop(-1) if len(self.front) - len(self.second) >= 2: self.second.insert(0,self.front.pop(-1)) return val # Your FrontMiddleBackQueue object will be instantiated and called as such: # obj = FrontMiddleBackQueue() # obj.pushFront(val) # obj.pushMiddle(val) # obj.pushBack(val) # param_4 = obj.popFront() # param_5 = obj.popMiddle() # param_6 = obj.popBack()