BEAT 98 USE EASY RECURSION


  • 0
    K
    public static String countAndSay(int n) {
            String res="1";
            for (int i=1;i<n;i++){
                res=Recursion(res);
            }
            return res;
        }
    
        public static String Recursion(String s){
            char[] cs=s.toCharArray();
            StringBuilder sb=new StringBuilder();
            char tmp;
            int count;
            for (int i=0;i<cs.length;i++){
                count=1;
                tmp=cs[i];
                while (i+1<cs.length&&cs[i+1]==tmp) {
                    count++;
                    i++;
                }
                sb.append(count).append(tmp);
            }
            return sb.toString();
        }
    

Log in to reply
 

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