본문 바로가기
카테고리 없음

leetcode 225. Implement Stack using Queues

by 무적김두칠 2023. 2. 23.

https://leetcode.com/problems/implement-stack-using-queues/

 

Implement Stack using Queues - LeetCode

Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the

leetcode.com

 

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
class MyStack:
 
    def __init__(self):
        self.q = collections.deque()
        
 
    def push(self, x: int-> None:
        self.q.append(x)
        for i in range(len(self.q)-1):
            self.q.append(self.q.popleft())
        
 
    def pop(self-> int:
        return self.q.popleft()
        
 
    def top(self-> int:
        return self.q[0]
        
 
    def empty(self-> bool:
        return len(self.q) == 0
        
 
 
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
cs
반응형

댓글