์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 232. implement_queue_using_stacks (Easy) 2023/5/10

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 5. 10. 02:24
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
232. implement_queue_using_stacks 63.4% Easy
 

Implement Queue using Stacks - LeetCode

Can you solve this real interview question? Implement Queue using Stacks - Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement t

leetcode.com

 

๋ฌธ์ œ์š”์•ฝ

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

  • def __init__(self):
  • def push(self, x:int)->None:
  • def pop(self)->int:
  • def peek(self)->int: # ํ์˜ ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ๋ฐ˜
  • def empty(self)->bool:

 

 

ํ’€์ด 1

์Šคํƒ ์—ฐ์‚ฐ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

 

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

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

steady-eschoi.tistory.com

 

 

๊ตฌํ˜„ (43ms)

class MyQueue:

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

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

    def pop(self) -> int:
        nowval=self.stack[0]
        del self.stack[0]
        return nowval

    def peek(self) -> int:
        return self.stack[0]

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