Very consise Java Solution, using two pointers.


  • 0
    Z
    public String countAndSay(int n) {
        String s = "1";
        while(--n > 0){
            s = getNextSeq(s);
        }
        return s;
    }
    public String getNextSeq(String s){
        String res = "";
        for(int i=0,j=0;i<s.length()&&j<=s.length();j++){
            if(j != s.length() && s.charAt(j) == s.charAt(i)) continue;
            res = res + (j-i) + s.charAt(i);
            i = j;
        }
        return res;
    }

Log in to reply
 

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