Why my code cannot pass "()" on OJ, it works perfect on my computer, thank you


  • 0
    R

    I used a separated sub-function called check(), thank you for your help

        global list1
        
        #short cut for the invalid at the beginning input
        if s[0] ==')' or s[0] == '}' or s[0] == ']':
            return False
        else:
            list1.append(s[0])
        #iteration
        for i in range(1, len(s)):
            result = list1.pop()
            if check(result, s[i]) == False:
                return False
        #final checking for an empty list 
        if not list1:
            return True
        else:
            return False
    
    
    
    list1 = []
    
    def check(result, input1):
        global list1
        if input1 == '(' or  input1 == '[' or input1 == '{':
            list1.extend([result, input1])
            return True
        elif (result == '(' and input1 == ')') or (result == '[' and input1 == ']') or (result == '{' and input1 == '}'):
            return True
        else:
            return False

Log in to reply
 

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