File tree 2 files changed +46
-1
lines changed
2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 68
68
| 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 ) |
69
69
| 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 ) |
70
70
| 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 ) |
72
72
| 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 ) |
73
73
| 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 ) |
74
74
| 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 ) |
Original file line number Diff line number Diff line change
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()
You can’t perform that action at this time.
0 commit comments