Java 4ms solution


  • 0
    A
        public String countAndSay(int n) {
            if(n==1) return "1";
            String s = countAndSay(n-1);
            StringBuilder sb = new StringBuilder();
            int i=0;
            while(i<s.length()){
                char c = s.charAt(i);
                char k='0';
                while(i<s.length() && s.charAt(i)==c){
                    k++;
                    i++;
                }
                sb.append(k);
                sb.append(c);
            }
            return sb.toString();
        }
    }

Log in to reply
 

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