1、题目
Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if the input string is valid.
An input string is valid if:
Note that an empty string is also considered valid.
Example 1:
Input: "()" Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]" Output: false
Example 4:
Input: "([)]" Output: false
Example 5:
Input: "{[]}"
Output: true
2、我的解答
# -*- coding: utf-8 -*-
# @Time : 2020/1/31 10:37
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 20. Valid Parentheses(copy大神做法).py
class Solution:
def isValid(self, s: str) -> bool:
bracket_map = {"(": ")", "[": "]", "{": "}"}
open_par = set(["(", "[", "{"])
stack = []
for i in s:
if i in open_par:
stack.append(i)
elif stack and i == bracket_map[stack[-1]]:
stack.pop()
else:
return False
return stack == []
print(Solution().isValid("{[[(])]}"))
leedCode练题——20. Valid Parentheses
原文:https://www.cnblogs.com/Smart-Cat/p/12245853.html