Skip to content

Commit dcd207e

Browse files
solves implement queue using stack
1 parent ac7f873 commit dcd207e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
| 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) |
6969
| 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) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/invert_binary_tree.py) |
7070
| 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) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_power_of_2.py) |
71-
| 232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) |
71+
| 232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_queue_using_stacks.py) |
7272
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) |
7373
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LowestCommonAncestorOfBinarySearchTree.java) |
7474
| 237 | [Delete a Node In A Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/DeleteANodeInLinkedList.java) |
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import deque
2+
3+
4+
class MyQueue:
5+
def __init__(self):
6+
"""
7+
Initialize your data structure here.
8+
"""
9+
self.s1 = deque()
10+
self.s2 = deque()
11+
12+
def push(self, x: int) -> None:
13+
"""
14+
Push element x to the back of queue.
15+
"""
16+
while len(self.s1) > 0:
17+
self.s2.append(self.s1.pop())
18+
self.s1.append(x)
19+
while len(self.s2) > 0:
20+
self.s1.append(self.s2.pop())
21+
22+
def pop(self) -> int:
23+
"""
24+
Removes the element from in front of queue and returns that element.
25+
"""
26+
return self.s1.pop()
27+
28+
def peek(self) -> int:
29+
"""
30+
Get the front element.
31+
"""
32+
return self.s1[-1]
33+
34+
def empty(self) -> bool:
35+
"""
36+
Returns whether the queue is empty.
37+
"""
38+
return len(self.s1) == 0
39+
40+
# Your MyQueue object will be instantiated and called as such:
41+
# obj = MyQueue()
42+
# obj.push(x)
43+
# param_2 = obj.pop()
44+
# param_3 = obj.peek()
45+
# param_4 = obj.empty()

0 commit comments

Comments
 (0)