232. Implement Queue using Stacks

Taesoo Kim·2023년 2월 7일
0

CrackingAlgorithm

목록 보기
22/36

문제링크: https://leetcode.com/problems/implement-queue-using-stacks/submissions/

전 포스팅의 반대인 스택을 이용한 큐 구현입니다.
스택은 하도 많이쓰는 구조여서 충분히 쉽게 구현 할 수 있을거 같습니다.

Problem

전 포스팅과 반대로 스택 -> 큐 입니다. push, pop, peek, empty를 구현하면 되고, peek은 pop과 다르게 최우선 아웃 값만 리턴해주면 됩니다.

Approach & Solution

설명을 하기도 민망한 코드입니다.ㅎㅎ

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()
profile
SailingToTheMoooon

0개의 댓글