[BOJ] 10828. ์คํ(python)
๐๋ฌธ์
๐ช์์ด๋์ด
- ๋ช
๋ น์ด ์ฒ๋ฆฌ
- push ๋ช ๋ น์ด ๋ค์ ์ซ์๋ฅผ ์คํ์ ๋ฃ์ด์ค๋ค
- size ์คํ๊ธธ์ด ์ถ๋ ฅ
stack ๋น ๊ณต๊ฐ์ธ์ง ๊ฒ์ฌ
ย stack O stack X empty 0์ถ๋ ฅ 1์ถ๋ ฅ pop stack.pop์ถ๋ ฅ -1์ถ๋ ฅ top stack[-1]์ถ๋ ฅ -1์ถ๋ ฅ
๐ฅ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sys
input=sys.stdin.readline
stack=[]
N = int(input())
for _ in range(N):
command = input().split()
if command[0]=='push':
stack.append(int(command[1]))
elif command[0]=='size':
print(len(stack))
else:
if stack:
if command[0] == 'pop': print(stack.pop())
elif command[0] == 'empty': print(0)
else: print(stack[-1])
else:
if command[0] == 'empty': print(1)
else: print(-1)
This post is licensed under CC BY 4.0 by the author.
Comments powered by Disqus.