Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- PytorchLightning
- pytorch
- 코딩테스트
- 완전탐색
- NLP
- 프로그래머스
- 알고리즘
- GIT
- wandb
- github
- NaverAItech
- 네이버AItech
- DeepLearning
- datascience
- python
- GCP
- pep8
- GitHub Action
- 백준
- rnn
- docker
- Kaggle
- FastAPI
- leetcode
- autoencoder
- Kubernetes
- Matplotlib
- FDS
- vscode
- torchserve
Archives
- Today
- Total
Sangmun
python 으로 stack 직접 구현하기 본문
[자료구조] 파이썬으로 스택(Stack)구현하기
파이썬(python)으로 스택(stack) 자료구조를 구현해보자파이썬에 내장되어 있는 Data-type중 리스트(list)를 이용하여 구현했다.Stack 클래스를 생성하고 init method를 이용하여 멤버 변수를 만들어준다.top
velog.io
스택 대표 문제
https://www.acmicpc.net/problem/4949
4949번: 균형잡힌 세상
하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다
www.acmicpc.net
while True :
a = input()
stack = []
if a == "." :
break
for i in a :
if i == '[' or i == '(' :
stack.append(i)
elif i == ']' :
if len(stack) != 0 and stack[-1] == '[' :
stack.pop() # 맞으면 지워서 stack을 비워줌 0 = yes
else :
stack.append(']')
break
elif i == ')' :
if len(stack) != 0 and stack[-1] == '(' :
stack.pop()
else :
stack.append(')')
break
if len(stack) == 0 :
print('yes')
else :
print('no')
'알고리즘 > 알고리즘(초급)' 카테고리의 다른 글
배낭 문제(냅색 문제) - 1 (분할가능한 배낭 문제) (0) | 2022.12.20 |
---|---|
python queue 구현 (0) | 2022.12.04 |
투포인터 (0) | 2022.11.23 |
백트래킹을 이용한 부분 집합을 구하는 알고리즘 (0) | 2022.11.21 |
유클리드 호제법(최대공약수), 최소공배수 (0) | 2022.11.18 |
Comments