my solution using formal grammar. long but elegant in my opinion...


  • 1
    A
    /**
     * grammar for the context.
     * NestedInteger           := Number | [InnerNestedIneger]
     * InnerNestedInteger      := NestedInteger | InnerNestedInteger,NestedInteger
     * Number                  := -PositiveNumber | PositiveNumber
     * PositiveNumber          := digit | digitPositiveNumber
     */
    public class Solution {
       
       private int index;
       private int sLen;
       
       public NestedInteger deserialize(String s) {
           sLen = s.length();
           if (sLen == 0) {
               return new NestedInteger();
           }
           index = 0;
           return nestedInteger(s);
       }
       
       public NestedInteger nestedInteger(String s) {
           
           if (leftBracket(s)) {
               NestedInteger result = innerNestedInteger(s);
               rightBracket(s);
               return result;
           } else {
               return integer(s);
           }
           
       }
       
       public NestedInteger innerNestedInteger(String s) {
           NestedInteger result = new NestedInteger();
           do {
               NestedInteger subResult = nestedInteger(s);
               if (subResult != null) {
                   result.add(subResult);
               }
           }  while (comma(s));
           return result;
       }
       
       public boolean leftBracket(String s) {
           if (index >= sLen) {
               return false;
           }
           if (s.charAt(index) == '[') {
               index++;
               return true;
           } else
               return false;
       }
       
       public NestedInteger integer(String s) {
           int posOrNeg = negtiveSign(s) ? -1 : 1;
           
           int num = positiveInteger(s); 
           if (num != -1) {
               num *= posOrNeg;
               return new NestedInteger(num);
           }
           
           return null;
       }
       
       public int positiveInteger(String s) {
           int num = 0;
           int digit = 0;
           boolean foundDigit = false;
           while (digit != -1) {
               digit = digit(s);
               if (digit > -1 && digit < 10) {
                   foundDigit = true;
                   num = num * 10 + digit;
               }
           }
           return foundDigit ? num : -1;
       }
       
       public boolean comma(String s) {
           if (index >= sLen) {
               return false;
           }
           if (s.charAt(index) == ',') {
               index++;
               return true;
           } else {
               return false;
           }
       }
       
       public int digit(String s) {
           if (index >= sLen) {
               return -1;
           }
           int result = s.charAt(index) - '0';
           if (result >= 0 && result <= 9) {
               index++;
               return result;
           } else {
               return -1;
           }
       }
       
       public boolean rightBracket(String s) {
           if (index >= sLen) {
               return false;
           }
           if (s.charAt(index) == ']') {
               index++;
               return true;
           } else {
               return false;
           }
       }
       
       public boolean negtiveSign(String s) {
           if (index >= sLen) {
               return false;
           }
           if (s.charAt(index) == '-') {
               index++;
               return true;
           } else {
               return false;
           }
       }
    }
    

  • 0
    A

    @alvisjiang This is awesome


Log in to reply
 

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