์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[LeetCode] 225. implement_stack_using_queues (Easy) 2023/5/9

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 5. 9. 21:18
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
225. implement_stack_using_queues 58.9% Easy
 

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

 

๋ฌธ์ œ์š”์•ฝ

ํ๋ฅผ ์ด์šฉํ•ด ์Šคํƒ์„ ๊ตฌํ˜„ํ•ด๋ผ

  • def __init__(self):
  • def push(self, x:int)->None:
  • def pop(self)->int:
  • def top(self)->int:
  • def empty(self)->bool:

 

ํ’€์ด 1

ํ ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด collections.deque()๋ฅผ ์‚ฌ์šฉ

๋””ํ ์—ฐ์‚ฐ์€ ๋‹ค์Œ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž.

 

[ํŒŒ์ด์ฌ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ] 1. ์Šคํƒ๊ณผ ํ

* ์ด ๊ธ€์€ ๋„ค์ด๋ฒ„ ๋ถ€์ŠคํŠธ ์ฝ”์Šค์˜ ์ธ๊ณต์ง€๋Šฅ(AI) ๊ธฐ์ดˆ ๋‹ค์ง€๊ธฐ ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ•ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์‚ฌ๋‹ด) ์–ผ๋งˆ์ „ ์ธํ„ด ๋ชจ์˜๋ฉด์ ‘์— ์šฐ์—ฐํžˆ ์ฐธ์—ฌํ•  ๊ธฐํšŒ๊ฐ€ ์ƒ๊ฒผ๋Š”๋ฐ ์Šคํƒ๊ณผ ํ๋ฅผ ํ—ท๊ฐˆ๋ ค๋ฒ„๋ ธ๋‹ค. ํ”„๋กœ์ 

steady-eschoi.tistory.com

 

 

๊ตฌํ˜„ (45ms)

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

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

    def pop(self) -> int:
        return self.q.pop()

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

    def empty(self) -> bool:
        return len(self.q)==0
728x90