python写算法题:leetcode: 20. Valid Parentheses

来源:互联网 发布:qq采集软件哪款好用 编辑:程序博客网 时间:2024/06/05 19:08

https://leetcode.com/problems/valid-parentheses/#/description

class Solution(object):    def isValid(self, s):        """        :type s: str        :rtype: bool        """        stab='(){}[]'        tab=[0]*(len(stab)/2)        pos=0        smap={}        ssig={}        fills=[]        for c in stab:            smap[c]=pos/2            ssig[c]=1-2*(pos%2)            pos+=1        for c in s:            pos=smap[c]            tab[pos]+=ssig[c]            if ssig[c]<0:                if len(fills)==0 or fills[-1]!=pos:                    return False                fills=fills[:-1]            else:                fills.append(pos)        if len(fills)!=0: return False        return True


原创粉丝点击