com / problems / valid-parentheses are copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. "" " " "" @author : jiyanjiao @date :2020-4-8 """ # 基本解法 @staticmethod def isValid(s): """ :type s: str :rtype: bool """ for i in s: if i == "(": index_begin = s.find("(") index_end = s.find(")") if index_end == -1: print("False") return index_r1 = s.find("[", index_begin, index_end) index_r2 = s.find("{", index_begin, index_end) index_r3 = s.find("]", index_begin, index_end) index_r4 = s.find("}", index_begin, index_end) if index_r1 == -1 and index_r2 == -1 and index_r3 == -1 and index_r4 == -1: print("True") else: print("False") elif i == "[": index_begin = s.find("[") index_end = s.find("]") index_r1 = s.find("(", index_begin, index_end) index_r2 = s.find("{", index_begin, index_end) index_r3 = s.find(")", index_begin, index_end) index_r4 = s.find("}", index_begin, index_end) if index_r1 == -1 and index_r2 == -1 and index_r3 == -1 and index_r4 == -1: print("True") else: print("False") elif i == "{": index_begin = s.find("{") index_end = s.find("}") index_r1 = s.find("(", index_begin, index_end) index_r2 = s.find("[", index_begin, index_end) index_r3 = s.find(")", index_begin, index_end) index_r4 = s.find("]", Index_begin, Index_end) IF index_r1 == -1 and index_r2 == -1 and index_r3 == -1 and index_r4 == -1 : Print ( " True " ) the else : Print ( " False " ) # other authors solution @staticmethod DEF isValid1 (S): "" " : type S: STR : rtype: BOOL " "" stack = [] # is provided a list to the list can be used as a stack. DIC = { ' ) ' : ' ( ' , ' } ' : ' { ' , ' ] ' : ' [ ' } # using dictionary storage bracket and the right bracket is key, a left parenthesis value for char in S: IF char in dic.values (): # left bracket on the stack stack.append (char) elif char in dic.keys (): # a right parenthesis, then to compare, IF Stack == [] or! dic [char] = stack.pop (): return False the else : return False # is no longer enter the dictionary directly output error return Stack == [] # If the last stack is empty, then it meets the requirements, output true, If not, then the output false, indicating that a conditional expression IF the __name__ == ' __main__ ' : S = Solution SS = " (] " s.isValid1 (SS)