출처: leetcode.com/problems/implement-queue-using-stacks/
문제
한글 번역은 아래 더보기를 클릭해주세요.
풀이
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = list()
def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.stack.append(x)
def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
peeked = self.peek();
self.stack = self.stack[1:]
return peeked
def peek(self) -> int:
"""
Get the front element.
"""
return self.stack[0] if len(self.stack) > 0 else None
def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return len(self.stack) == 0
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
결과
'CS기초 > 코딩테스트' 카테고리의 다른 글
LeetCode 1209 (Easy) Remove All Adjacent Duplicates in String II (0) | 2021.03.23 |
---|---|
LeetCode 20 (Easy) Valid Parentheses (0) | 2021.03.23 |
이것이 취업을 위한 코딩테스트다 15장. 이진탐색 문제 (0) | 2021.03.11 |
LeetCode 35 (Easy) Search Insert Position (0) | 2021.03.11 |
LeetCode 349 (Easy) Intersection of Two Arrays (0) | 2021.03.11 |