Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac0f7d9

Browse files
add 225
1 parent c22fe0e commit ac0f7d9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class MyStack(object):
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self._top = None
8+
self._q1 = []
9+
self._q2 = []
10+
11+
12+
def push(self, x):
13+
"""
14+
Push element x onto stack.
15+
:type x: int
16+
:rtype: void
17+
"""
18+
self._top = x
19+
q = self._q1 or self._q2
20+
q.append(x)
21+
22+
23+
def pop(self):
24+
"""
25+
Removes the element on top of the stack and returns that element.
26+
:rtype: int
27+
"""
28+
q = self._q1 or self._q2
29+
if not self._q1:
30+
another = self._q1
31+
else:
32+
another = self._q2
33+
34+
while len(q) > 1:
35+
x = q.pop(0)
36+
self._top = x
37+
another.append(x)
38+
return q.pop()
39+
40+
def top(self):
41+
"""
42+
Get the top element.
43+
:rtype: int
44+
"""
45+
return self._top
46+
47+
def empty(self):
48+
"""
49+
Returns whether the stack is empty.
50+
:rtype: bool
51+
"""
52+
return self._q1 and self._q2
53+
54+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /