java实现,注释清楚


  • 0
    C

    public String countAndSay(int n) {
    if(n==1) return "1";
    String str=countAndSay(n-1),sout="";
    int count=1,length=str.length();
    char[] result=str.toCharArray();
    for(int i=0;i<length;i++){
    if(i!=length-1 && (result[i]==result[i+1])) count++; //保证最后一次一定要加1,同时计算出前面的个数,
    else{
    sout=sout+count+result[i]; //这里是添加
    count=1; //重新开始计算
    }
    }
    return sout;
    }


Log in to reply
 

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