AC clean C++ solution


  • 0
    class Solution {
    public:
        NestedInteger deserialize(const string& s) {
            NestedInteger ni;
            if (isdigit(s[i]) || s[i] == '-') { // parse num
                int j = i++;
                while (i < s.length() && isdigit(s[i])) i++;
                ni.setInteger(stoi(s.substr(j, i - j)));
            }
            else if (i + 1 < s.length() && s[i] == '[' && s[i + 1] == ']') i += 2; // parse empty list
            else while (i < s.length() && s[i++] != ']') ni.add(deserialize(s)); // parse list
            return ni;
        }
    private:
        int i = 0;
    };
    

Log in to reply
 

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