forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAC_front_back_stack_1.py
47 lines (38 loc) · 1 KB
/
AC_front_back_stack_1.py
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
46
47
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_front_back_stack_1.py
# Create Date: 2015-07-27 09:52:54
# Usage: AC_front_back_stack_1.py
# Descripton:
class Queue:
# initialize your data structure here.
def __init__(self):
self.front = []
self.back = []
def move_back_to_front(self):
while self.back:
self.front.append(self.back[-1])
self.back.pop()
# @param x, an integer
# @return nothing
def push(self, x):
self.back.append(x)
# @return nothing
def pop(self):
if not self.front:
self.move_back_to_front()
self.front.pop()
# @return an integer
def peek(self):
if not self.front:
self.move_back_to_front()
return self.front[-1]
# @return an boolean
def empty(self):
return not self.back and not self.front
# test
q = Queue()
q.push(1)
print q.empty()
print q.peek()