알고리즘/큐, 스택
11. 스택 - 유효한 괄호
jwjwvison
2021. 8. 26. 19:29
https://leetcode.com/problems/valid-parentheses/submissions/
Valid Parentheses - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
괄호로 된 입력값이 유효한지 판별하라.
전형적인 스택 문제로 매우 쉬우면서도 기본기를 점검할 수 있는 좋은 문제이다.
class Solution:
def isValid(self, s: str) -> bool:
d={'}':'{',']':'[',')':'('}
stack=[]
for let in s:
if let not in d:
stack.append(let)
elif not stack or d[let] != stack.pop():
return False
return len(stack)==0
딕셔너리로 위와 같이 key-value 쌍을 지정해준다.
딕셔너리에서 in 함수를 사용할때 기준은 key값이다. value값은 in으로 찾을 수 없다.