Problem Solving
71. Implement Queue using Stacks
굥깡
2023. 1. 14. 19:39
728x90
Implement Queue using Stacks - LeetCode
Implement Queue using Stacks - Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: * void push(int x) Pushes
leetcode.com
Queue class 안에 queue의 역할을 할 수 있도록 push, pop 등의 함수를 구현하는 문제
class MyQueue:
def __init__(self):
self.li = []
def push(self, x: int) -> None:
self.li.append(x)
def pop(self) -> int:
return self.li.pop(0)
def peek(self) -> int:
return self.li[0]
def empty(self) -> bool:
if len(self.li) == 0:
return True
else:
return False
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
input 형식이 이해가 안 가서 이게 맞나...? 하면서 짜놓고 일단 제출했는데 accept 됨