My java solution


  • 0
    L
    public String countAndSay(int n) {
        if(n<=0) return "";
        String sayBefore="1";
        StringBuilder say=new StringBuilder("1");
        int count=0;
        for(int i=2;i<=n;i++){
            count=1;
            say=new StringBuilder();
            for(int j=1;j<sayBefore.length();j++){
                if(sayBefore.charAt(j)!=sayBefore.charAt(j-1)){
                    say.append(count).append(sayBefore.charAt(j-1));
                    count=1;
                }
                else count++;
            }
            say.append(count).append(sayBefore.charAt(sayBefore.length()-1));
            sayBefore=say.toString();
        }
        return say.toString();
    }

Log in to reply
 

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