문제링크: https://leetcode.com/problems/implement-queue-using-stacks/submissions/
전 포스팅의 반대인 스택을 이용한 큐 구현입니다.
스택은 하도 많이쓰는 구조여서 충분히 쉽게 구현 할 수 있을거 같습니다.
전 포스팅과 반대로 스택 -> 큐 입니다. push, pop, peek, empty를 구현하면 되고, peek은 pop과 다르게 최우선 아웃 값만 리턴해주면 됩니다.
설명을 하기도 민망한 코드입니다.ㅎㅎ
class MyQueue:
def __init__(self):
self.stack = []
def push(self, x: int) -> None:
return self.stack.append(x)
def pop(self) -> int:
temp = self.stack[0]
self.stack = self.stack[1:]
return temp
def peek(self) -> int:
return self.stack[0]
def empty(self) -> bool:
return len(self.stack) == 0
# 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()