월요일 아침을 여는 리트코드!
오늘은 쉬운 문제로 선택했다. (연구가 시급해서... 논문도 덜읽었어...)
Valid parentheses이다.
class Solution:
def isValid(self, s: str) -> bool:
parentheses = {')': '(', '}': '{', ']' : '['}
stack = []
for c in s:
if c in parentheses.values():
stack.append(c)
if c in parentheses.keys():
if stack == []:
return False
elif parentheses[c] != stack.pop():
return False
return stack == []
코드는 stack을 이용하는데, 가령 {가 마지막에 들어왔으면 닫는 기호 중 }가 나와줘야 valid하므로 stack.pop을 이용하였다.
또한 parentheses dictionary를 반대로 이용한 것은, parentheses[c]로 쉽게 찾기 위해서이다.
끝!
완료했다 히히
오늘도 파이팅!
'Leetcode' 카테고리의 다른 글
Leetcode 268. Missing Number (0) | 2021.11.23 |
---|---|
Leetcode 1137. N-th Tribonacci Number (0) | 2021.11.16 |
Leetcode 122. Best Time to Buy and Sell Stock II (0) | 2021.11.10 |
Leetcode 118. Pascal's Triangle (0) | 2021.11.06 |
441. Arranging Coins (0) | 2021.11.05 |