문제
https://www.acmicpc.net/problem/10828
걸린 시간
00 : 40 : 37
풀이
Python3
class Stack:
def __init__(self):
self.stack = []
def push(self, X):
self.stack.append(X)
def pop(self):
if self.empty():
return -1
else:
return self.stack.pop()
def size(self):
return len(self.stack)
def empty(self):
if self.size() == 0:
return 1
else:
return 0
def top(self):
if self.empty():
return -1
else:
return self.stack[-1]
if __name__ == "__main__":
# 명령의 수 : N
N = int(input())
# 명령어 list
command = []
# 스택 객체 생성
s = Stack()
for i in range(0, N):
command.append(input())
for i in command:
# push 명령이면
c = i.split()
if len(c) > 1:
s.push(c[1])
else:
if c[0] == "pop":
print(s.pop())
elif c[0] == "size":
print(s.size())
elif c[0] == "empty":
print(s.empty())
elif c[0] == "top":
print(s.top())
입력받은 명령어를 어떻게 실행시킬지 생각하느라 조금 헤맸다.
top 메소드 구현 시 리스트의 맨 마지막 값 [-1]
을 어떻게 출력하는지 까먹었었다.
'Baekjoon' 카테고리의 다른 글
Baekjoon 1002번 터렛 (0) | 2020.07.20 |
---|---|
Baekjoon 10848번 큐 (0) | 2020.07.19 |
Baekjoon 18111번 마인크래프트 (0) | 2020.07.18 |
Baekjoon 2805번 나무 자르기 (0) | 2020.07.17 |
Baekjoon 1654번 랜선 자르기 (0) | 2020.07.16 |
댓글