My clean java solution


  • 0
    C
    public class Solution {
    public String countAndSay(int n) {
        if(n==1) return "1";
        String s = String.valueOf(1);
        for(int i=2; i<=n; i++) {
            StringBuilder sb = new StringBuilder();
            for(int j=0; j<s.length(); j++) {
                char c = s.charAt(j);
                int count = 1;
                while(j+1<s.length() && s.charAt(j+1)==c) {
                    j = j + 1;
                    count ++;
                }
                sb.append(count).append(c);
            }
            s = sb.toString();
        }
        return s;
    }
    

    }


Log in to reply
 

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