美文网首页
38. Count and Say

38. Count and Say

作者: 夜皇雪 | 来源:发表于2016-12-12 07:19 被阅读0次
    public class Solution {
        public String countAndSay(int n) {
            int i=1;
            String seq="1";
            while(i<n){
                StringBuilder sb=new StringBuilder();
                int count=0;
                char c=seq.charAt(0);
                for(int j=0;j<=seq.length();j++){
                    if(j!=seq.length()&&seq.charAt(j)==c){
                        count++;
                        continue;
                    }else{
                        sb.append(count);
                        sb.append(c);
                        if(j!=seq.length()){
                            c=seq.charAt(j);
                            count=1;
                        }
                    }
                }
                seq=sb.toString();
                i++;
            }
            return seq;
        }
    }
    

    相关文章

      网友评论

          本文标题:38. Count and Say

          本文链接:https://www.haomeiwen.com/subject/rrcmmttx.html