use list to replace stack in python


  • 0
    Y
    class Solution:
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            D = {"(":-1,")":1,"[":-2,"]":2,"{":-3,"}":3}
            S = []
            for i in range(len(s)):
                S.pop() if len(S)!=0 and D[S[-1]] == -D[s[i]] else S.append(s[i])
            return True if len(S) ==0 else False
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.