자료구조알고리즘/문제풀이
[신찬수] 괄호 맞추기
포시
2022. 12. 16. 10:12
728x90
문제
https://www.youtube.com/watch?v=OzFXiukhv8o&list=PLsMufJgu5933ZkBCHS7bQTx0bncjwi4PK&index=9
내 풀이
str = '(()))('
def test(str):
stack = []
try:
for char in str:
# print(char)
if char == '(':
stack.append(char)
else:
stack.pop()
if len(stack) > 0:
return False # '(' 더 많음
else:
return True
except:
return False # ')' 더 많음
result = test(str)
print(result)
stack을 이용해 괄호'()' 의 짝을 맞추는 문제를 효율적으로 푸는 방법에 대해 배웠다.
728x90