๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
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