Easy to Understand java solution


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

Log in to reply
 

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