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        

 

posted @ 2017-12-16 16:18  PirateLHX  阅读(129)  评论(0编辑  收藏  举报