๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
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
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 641. design_circular_deque (Medium) 2023/5/11 (0) | 2023.05.11 |
---|---|
[LeetCode] 622. design_circular_queue (Medium) 2023/5/10 (0) | 2023.05.10 |
[LeetCode] 739. daily_temperatures (Medium) 2023/5/9 (0) | 2023.05.09 |
[LeetCode] 316. remove_duplicate_letters (Medium) 2023/5/8 (0) | 2023.05.08 |
[LeetCode] 20. Valid_Parentheses (Easy) 2023/5/8 (0) | 2023.05.08 |