20. Valid Parentheses

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        l=[]
        candidate=['(',')','{','}','[',']']
        d={'(':')','{':'}','[':']',')':0,']':0,'}':0}
        for i in range(len(s)):
            if s[i] in candidate:
                if len(l)==0:
                    l.append(s[i])
                else:
                    if(d[l[len(l)-1]]==s[i]):
                        del l[len(l)-1]
                    else:
                        l.append(s[i])
            else:
                break
        if len(l)==0:
            return True
        else:
            return False        
原文地址:https://www.cnblogs.com/xlqtlhx/p/8046858.html