π
νμ΄μ¬ μκ³ λ¦¬μ¦ μΈν°λ·°
μ± μ μ°Έκ³ νμ΅λλ€.
νλ‘ μ€νμ μ€νμΌλ‘ νλ₯Ό ꡬννμΈμ.
νλ‘ μ€ν ꡬννκΈ°
νμ¬ κ° μμ μλ κ²λ€μ νμμ λΉΌμ νμ¬ κ° λ€λ‘ μ½μ
νλ€.
μ€νμΌλ‘ ν ꡬννκΈ°
μ€ν λ κ°λ₯Ό μ΄μ©ν΄ pushλ μ μ₯λ§μ μννλ input μ€νμ λ£κ³
popνλμ ν΅ν΄ outputμ€νμ κ°μ΄ μλ€λ©΄ inputμ λ°λλ‘ κ°μ λ°μμ€λλ‘ νλ€.
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.q = collections.deque()
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.q.append(x)
for _ in range(len(self.q) - 1):
self.q.append(self.q.popleft())
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
return self.q.popleft()
def top(self) -> int:
"""
Get the top element.
"""
return self.q[0]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return len(self.q) == 0
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.tmp = []
while self.stack:
self.tmp.append(self.stack.pop())
self.stack.append(x)
while self.tmp:
self.stack.append(self.tmp.pop())
return self.stack
def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
return self.stack.pop()
def peek(self) -> int:
"""
Get the front element.
"""
return self.stack[-1]
def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return len(self.stack) == 0