Java dfs, beats 98%


  • 0
    V
        int itr = 0;
        public NestedInteger deserialize(String s) 
        {
            return dfs(s);
        }
        NestedInteger dfs(String s)
        {
            if(itr >= s.length())
                return null;
            int val = 0, sign = 1;
            NestedInteger toRet = new NestedInteger();
            if(s.charAt(itr) != '[')
            {
                if(s.charAt(itr) == '-')
                {
                    itr++;
                    sign = -1;
                }
                while(itr < s.length() && s.charAt(itr) <= 57 && s.charAt(itr) >= 48)
                {
                    val *= 10;
                    val += s.charAt(itr ++) - 48;
                }
                val *= sign;
                toRet = new NestedInteger(val);
                return toRet;
            }
            int prev = itr;        
            itr++;
            while(s.charAt(itr) != ']')
            {
                toRet.add(dfs(s));
                if(s.charAt(itr) == ',')
                    itr++;
            }
            itr++;
            return toRet;
        }

Log in to reply
 

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