ZZIN33
re-code-cord
ZZIN33
전체 방문자
오늘
어제
  • 분류 전체보기 (52)
    • Paper (4)
      • Generative Model (2)
      • Segmentation (1)
      • 모델 경량화 (1)
    • Study (34)
      • AI (10)
      • MLOps (8)
      • CS (4)
      • OpenCV (1)
      • Algorithm (9)
      • ETC (2)
    • Project (6)
    • ETC (8)
      • 부스트캠프 AI Tech (2)
      • 도서 리뷰 (5)

블로그 메뉴

  • Home
  • About
  • Github

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ZZIN33

re-code-cord

[백준] 10828번: 스택 (Python)
Study/Algorithm

[백준] 10828번: 스택 (Python)

2021. 11. 25. 18:26

풀이

import sys

cnt = int(sys.stdin.readline())
stack = []

for i in range(cnt):
    command = sys.stdin.readline().split()

    if command[0] == "push":
      stack.append(command[1])
    
    elif command[0] == "pop":
      if len(stack):
        print(stack.pop())
      else:
        print(-1)

    elif command[0] == "size":
      print(len(stack))

    elif command[0] == "empty":
      if len(stack):
        print(0)
      else:
        print(1)

    elif command[0] == "top":
      if len(stack):
        print(stack[-1])
      else:
        print(-1)
C++에서는 stack 기본 함수로, push(), top() 등이 있었는데, 파이썬에는 없어서 순간 당황했다.
>> 파이썬에는 비슷하게 collections에 deque라는 것이 있다고 한다.

 

저작자표시 (새창열림)

'Study > Algorithm' 카테고리의 다른 글

[프로그래머스] 주식가격 (Python)  (0) 2021.12.02
[백준] 9663번 : N-Queen (Python)  (0) 2021.11.30
[백준] 5430번: AC (Python)  (1) 2021.11.29
[백준] 1920번: 수 찾기, 10815번: 숫자 카드 (Python)  (0) 2021.11.25
[백준] 2108번: 통계학 (Python)  (0) 2021.11.24

    티스토리툴바