거누의 개발노트

파이썬 알고리즘 인터뷰 - 큐를 이용한 스택 구현 본문

코딩테스트

파이썬 알고리즘 인터뷰 - 큐를 이용한 스택 구현

Gogozzi 2022. 5. 17. 13:17
반응형

문제

큐를 이용해 다음 연산을 지원하는 스택을 구현하라.

초기 셋팅

class MyStack:
    def __init__(self):


    def push(self, x:int):


    def top(self):


    def pop(self):


    def empty(self):

입력

["MyStack","push","push","top","pop","empty"]
[[],[1],[2],[],[],[]]

출력

[null,null,null,2,2,false]

작성한 코드

import collections
class MyStack:
    def __init__(self):
        self.q = collections.deque()

    def push(self, x:int):
        return self.q.append(x)

    def top(self):
        return self.q[-1]

    def pop(self):
        return self.q.pop()


    def empty(self):
        return not self.q

 

리코드에서 확인

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

 

Implement Stack using Queues - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

회고

큐를 이용하면 사용할 수 있는 함수들이 있어서 비교적 간단하게 풀었다.

반응형
Comments