No stack, no string cat.


  • 0
    public class Solution {
        private int i = 0;
        
        public String parseTernary(String expression) {
            char c = expression.charAt(i);
            if ('0' <= c && c <= '9') {
                i += 1;
                return String.valueOf(c);
            }
            if (i == expression.length() - 1 || expression.charAt(i + 1) == ':') {
                i += 1;
                return String.valueOf(c);
            }
            i += 2;
            String first = parseTernary(expression);
            i += 1;
            String second = parseTernary(expression);
            return c == 'T' ? first : second;
        }
    }
    

    Use a global var i to record where we are right now.


Log in to reply
 

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