Valid Parentheses

 1 class Solution(object):
 2     def isValid(self, s):
 3         """
 4         :type s: str
 5         :rtype: bool
 6         """
 7         dic={')':'(',']':'[','}':'{'}
 8         stack=[]
 9         for i in range(len(s)):
10             if(s[i] in dic.values()):
11                 stack.append(s[i])
12             else:
13                 if(len(stack)==0):
14                     return False
15                 else:
16                     if(dic[s[i]]!=stack[-1]):
17                         return False
18                     else:
19                         stack.pop()
20             
21         return len(stack)==0
原文地址:https://www.cnblogs.com/zijidan/p/12535475.html