C++ mini parser solution


  • 0
    N
     * // This is the interface that allows for creating nested lists.
     * // You should not implement it, or speculate about its implementation
     * class NestedInteger {
     *   public:
     *     // Constructor initializes an empty nested list.
     *     NestedInteger();
     *
     *     // Constructor initializes a single integer.
     *     NestedInteger(int value);
     *
     *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
     *     bool isInteger() const;
     *
     *     // Return the single integer that this NestedInteger holds, if it holds a single integer
     *     // The result is undefined if this NestedInteger holds a nested list
     *     int getInteger() const;
     *
     *     // Set this NestedInteger to hold a single integer.
     *     void setInteger(int value);
     *
     *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
     *     void add(const NestedInteger &ni);
     *
     *     // Return the nested list that this NestedInteger holds, if it holds a nested list
     *     // The result is undefined if this NestedInteger holds a single integer
     *     const vector<NestedInteger> &getList() const;
     * };
     */
    class Solution {
    public:
        NestedInteger deserialize(string const& s) {
            int cursor = 0;
            return parserListR(s, cursor);
        }
        
    private:
        NestedInteger parserListR(string const& s, int& cursor)
        {
            NestedInteger result;
            
            if(isDigit(s[cursor]) || s[cursor] == '-')
            {
                result.setInteger(parseNumber(s, cursor));
                return result;
            }
            
            ++cursor;
            while(cursor < s.length())
            {
                if(s[cursor] == '[')
                {
                    result.add(parserListR(s, cursor));
                }
                
                if(isDigit(s[cursor]) || s[cursor] == '-')
                {
                    result.add(NestedInteger(parseNumber(s, cursor)));
                }
                
                if(s[cursor] == ']')
                {
                    ++cursor;
                    return result;
                }
                
                ++cursor;
            }
            
            return result;
        }
        
        int parseNumber(string const& s, int &cursor)
        {
            int result = 0;
            int multiplier = 1;
            
            if(s[cursor] == '-')
            {
                multiplier = -1;
                ++cursor;
            }
            
            while(isDigit(s[cursor]))
            {
                result = result*10 + (s[cursor++] - '0'); 
            }
            
            return result * multiplier;
        }
        
        static bool isDigit(char ch)
        {
            return ch >= '0' && ch <= '9';
        }
    };```

Log in to reply
 

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