IT/알고리즘
[Leetcode]20. Valid Parentheses
어센트
2020. 10. 6. 14:39
가장 기본적인 문제였다. 풀이를 보니 딕셔너리를 이용해서 해결한 것 같은데 집에 가면 책을 참고하면서 다시 풀이를 확인해야겠다.
내 풀이 (분명히 저번에 풀어본 문젠데 코드가 상당히 더럽게 나왔다....)
def isValid(self, s: str) -> bool:
stack = []
for c in s:
if c == '(' or c == '{' or c == '[':
stack.append(c)
elif stack:
if c == ')' and '(' == stack[-1]:
stack.pop()
elif c == '}' and '{' == stack[-1]:
stack.pop()
elif c == ']' and '[' == stack[-1]:
stack.pop()
else:
return False
else: return False
return not stack