225. Implement Stack using Queues
https://youtu.be/rW4vm0-DLYc?si=HfNFNoJYw5i3MRzp
class MyStack:
def __init__(self):
self.q = deque()
def push(self, x: int) -> None:
self.q.append(x)
def pop(self) -> int:
# 삭제해야할 값은. 큐에 마지막 데이터 이므로
# 마지막 전까지는 우선 다시 큐에 담고
for i in range(len(self.q) -1):
self.q.append(self.q.popleft())
# 마지막 데이터는 pop 하면서 삭제
return self.q.popleft()
def top(self) -> int:
return self.q[-1]
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()
'LeetCode > 주제별 보충' 카테고리의 다른 글
Sorting: 75. Sort Colors (0) | 2025.02.04 |
---|---|
LinkedList (Queue): 1700. Number of Students Unable to Eat Lunch ★ (0) | 2025.02.01 |
Graphs (Union Find): 684. Redundant Connection (1) | 2025.01.28 |
Graphs(싸이클확인, Union Find): 323. Number of Connected Components in an Undirected Graph ★★★ (0) | 2025.01.28 |
List(싸이클탐지): 142. Linked List Cycle II (0) | 2025.01.27 |