java 2 ms easy-understand


  • 0
    E

    public String countAndSay(int n) {
    if(n==1) return "1";
    String str=countAndSay(n-1);
    char[] chs=str.toCharArray();
    StringBuilder sb=new StringBuilder();
    int num=1,len=chs.length;
    for(int i=0;i<len;i++){
    while(i+1<len&&chs[i]==chs[i+1]){
    num++;
    i++;
    }
    sb.append(num).append(chs[i]);
    num=1;
    }
    return sb.toString();
    }
    不会排版

    • [list item](link url)

  • 0
    C

    good-to-read version

    public String countAndSay(int n) {
        if(n==1) return "1";
        String str=countAndSay(n-1);
        char[] chs=str.toCharArray();
        StringBuilder sb=new StringBuilder();
        int num=1,len=chs.length;
        for(int i=0;i<len;i++){
            while(i+1<len&&chs[i]==chs[i+1]){
                num++;
                i++;
            }
            sb.append(num).append(chs[i]);
            num=1;
        }
        return sb.toString();
    }
    

    我帮你排版


Log in to reply
 

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