Share my java solution


  • 0
    J
        public String countAndSay(int n) {
            String s = "1";
            for(int j=2; j<=n; j++){
                StringBuilder sb = new StringBuilder();
                int length = 0;
                for(int i=0; i<s.length(); i++){
                    length++;
                    if(i == s.length() - 1 || s.charAt(i) != s.charAt(i+1)){
                        sb.append(length);
                        sb.append(s.charAt(i));
                        length = 0;
                    }
                }
                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.