https://leetcode.com/problems/valid-parentheses/
괄호가 정상적인지 찾는 스택의 대표 문제
class Solution:
def isValid(self, s: str) -> bool:
types = { ')' : '(', ']' : '[', '}': '{' }
stack = []
for par in s:
if par in types:
if not stack or types[par] != stack.pop():
return False
else:
stack.append(par)
return len(stack) == 0
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] Number of Islands (0) | 2022.03.25 |
---|---|
[LeetCode] Two City Scheduling (0) | 2022.03.25 |
[LeetCode] Swap Nodes in Pairs (0) | 2022.03.24 |
[LeetCode] Boats to Save People (0) | 2022.03.24 |
[LeetCode] Add Two Numbers (0) | 2022.03.23 |
댓글