티스토리 뷰
문제
스택을 이용해 다음 연살을 지원하는 큐를 구현하라.
- push(x): 요소 x를 큐 마지막에 삽입한다.
- pop(): 큐 처음에 있는 요소를 제거한다.
- peek(): 큐 처음에 있는 요소를 조회한다.
- empty(): 큐가 비어 있는지 여부를 리턴한다.
leetcode 232 - Implement Queue using Stacks
코드
class MyQueue:
def __init__(self):
self.input = []
self.output = []
def push(self, x: int) -> None:
self.input.append(x)
def pop(self) -> int:
self.peek()
return self.output.pop()
def peek(self) -> int:
# output이 없으면 모두 재입력
if not self.output:
while self.input:
self.output.append(self.input.pop())
return self.output[-1]
def empty(self) -> bool:
return self.input == [] and self.output == []
# 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()
'알고리즘' 카테고리의 다른 글
[algorithm] leetcode 641 - Design Circular Deque (파이썬) (0) | 2022.03.25 |
---|---|
[algorithm] leetcode 20 - Valid Parentheses (파이썬) (0) | 2022.03.24 |
[algorithm] leetcode 225 - Implement Stack using Queues (파이썬) (0) | 2022.03.22 |
[algorithm] 프로그래머스 - 없는 숫자 더하기 (파이썬) (0) | 2022.03.20 |
[algorithm] 프로그래머스 - 완주하지 못한 선수 (파이썬) (0) | 2022.03.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 김영한
- spring boot
- 노마드
- 인프런
- 리팩토링
- 릿코드
- 자료구조
- 문자열
- Real MySQL
- 스프링
- 스프링 부트
- 스프링부트
- MySQL
- 백준
- 알고리즘
- Spring
- kotlin
- 데이터베이스
- 구현
- 파이썬
- webflux
- 그리디
- 정렬
- mysql 8.0
- 북클럽
- Algorithm
- 노마드코더
- 코틀린
- 코테
- leetcode
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함