Skip to content

Commit 9a6604e

Browse files
implement stack using queues
1 parent 16c49c1 commit 9a6604e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
| 206 | [Reverse Linked Lists](https://leetcode.com/problems/reverse-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ReverseLinkedList.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_linked_list.py) |
6464
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ContainsDuplicate.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/contains_duplicate.py) |
6565
| 219 | [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ContainsDuplicateII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/contains_duplicate_ii.py) |
66-
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyStack.java) |
66+
| 225 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyStack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_stack_using_queues.py) |
6767
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/InvertBinaryTree.java) |
6868
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) |
6969
| 232 | [Implement Queue Using Stacks]() | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) |

Diff for: python/implement_stack_using_queues.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import deque
2+
3+
4+
class MyStack:
5+
6+
def __init__(self):
7+
"""
8+
Initialize your data structure here.
9+
"""
10+
self.queue = deque()
11+
12+
def push(self, x: int) -> None:
13+
"""
14+
Push element x onto stack.
15+
"""
16+
self.queue.append(x)
17+
18+
def pop(self) -> int:
19+
"""
20+
Removes the element on top of the stack and returns that element.
21+
"""
22+
return self.queue.pop()
23+
24+
def top(self) -> int:
25+
"""
26+
Get the top element.
27+
"""
28+
return self.queue[-1]
29+
30+
def empty(self) -> bool:
31+
"""
32+
Returns whether the stack is empty.
33+
"""
34+
return len(self.queue) == 0
35+
36+
# Your MyStack object will be instantiated and called as such:
37+
# obj = MyStack()
38+
# obj.push(x)
39+
# param_2 = obj.pop()
40+
# param_3 = obj.top()
41+
# param_4 = obj.empty()

0 commit comments

Comments
 (0)