Post My Java Solution, Recursive, Very Concise


  • 0
    J
    public class Solution {
    public String countAndSay(int n) {
        if(n==1) return Integer.toString(1);
        
        String input = countAndSay(n-1);
        StringBuilder res = new StringBuilder();
        char[] num = input.toCharArray();
        int i=0,j=0,count=0;
        
        while(j<num.length){
            count = 0;
            while(j<num.length&&num[i]==num[j]){
                count++;
                j++;
            }
            res.append(Integer.toString(count)).append(num[i]);
            i = j;
        }
        return res.toString();
    }
    

    }


Log in to reply
 

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