-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignFrontMiddleBackQueue.py
More file actions
45 lines (34 loc) · 1.03 KB
/
designFrontMiddleBackQueue.py
File metadata and controls
45 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/design-front-middle-back-queue/
# Author: Miao Zhang
# Date: 2021-05-26
class FrontMiddleBackQueue:
def __init__(self):
self.q = []
def pushFront(self, val: int) -> None:
self.q.insert(0, val)
def pushMiddle(self, val: int) -> None:
self.q.insert(len(self.q) // 2, val)
def pushBack(self, val: int) -> None:
self.q.append(val)
def popFront(self) -> int:
if self.q:
return self.q.pop(0)
return -1
def popMiddle(self) -> int:
if self.q:
return self.q.pop((len(self.q) - 1) // 2)
return -1
def popBack(self) -> int:
if self.q:
return self.q.pop(-1)
return -1
# 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()