23ms recursive solution


  • 0
    F
    class Solution {
    public:
        char* parse(char *p, NestedInteger &ni)
        {
            if(*p != '[') {
                //integer
                int v = strtol(p, &p, 10);
                ni.setInteger(v);
                return p;
            }
    
            ++p;
            while(*p != ']') {
                //list
                while(*p == ',') ++p;
                NestedInteger tmpNI;
                p = parse(p, tmpNI);
                ni.add(tmpNI);
            }
            return p+1; //skip ']'
        }
    
        NestedInteger deserialize(string s) {
            char *p = (char*)s.c_str();
            NestedInteger ans;
            parse(p, ans);
            return ans;
        }
    };

Log in to reply
 

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