Python recursive solution


  • 1
    J
    class Solution(object):
        def deserialize(self, s):
            """
            :type s: str
            :rtype: NestedInteger
            """
            try:
                return int(s)
            except:
                i, open_paren, result = 1, 0, []
                for j in range(1, len(s)-1):
                    if s[j] == ',' and not open_paren:
                        result.append(s[i:j])
                        i = j+1
                    elif s[j] == '[':
                        open_paren += 1
                    elif s[j] == ']':
                        open_paren -= 1
                if i < len(s)-1:
                    result.append(s[i:len(s)-1])
                
                for i in range(len(result)):
                    result[i] = self.deserialize(result[i])
                
                return result
                
    

Log in to reply
 

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