파비의 매일매일 공부기록

2023.08.28 Today's Challenge 본문

Problem Solving/LeetCode

2023.08.28 Today's Challenge

fabichoi 2023. 8. 28. 23:45

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

 

Implement Stack using Queues - LeetCode

Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the

leetcode.com

리스트로 매우 간단하게 구현 가능

class MyStack:

    def __init__(self):
        self.st = []

    def push(self, x: int) -> None:
        self.st.append(x)

    def pop(self) -> int:
        return self.st.pop(-1)

    def top(self) -> int:
        return self.st[-1]

    def empty(self) -> bool:
        return False if len(self.st) else True
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.08.30 Today's Challenge  (0) 2023.08.30
2023.08.29 Today's Challenge  (0) 2023.08.29
2023.08.27 Today's Challenge  (0) 2023.08.27
2023.08.26 Today's Challenge  (0) 2023.08.26
2023.08.25 Today's Challenge  (0) 2023.08.25
Comments