Problem#20 Valid Parentheses

Problem

在這裏插入圖片描述

Solution

class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        map = {
            ')': '(',
            '}': '{',
            ']': '['
        }
        if len(s) == 0:
            return True
        for str in s:
            if str in map.values():  # 左括號
                stack.append(str)
            elif str in map.keys():  # 右括號
                if len(stack) == 0:
                    return False
                else:
                    if stack[-1] == map[str]:
                        stack.pop()
                    else:
                        return False
        return len(stack)==0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章