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

 

posted @ 2020-03-20 22:39  7aughing  阅读(104)  评论(0编辑  收藏  举报