38. Count and Say

My Submissions

Total Accepted: 110903
Total Submissions: 343285
Difficulty: Easy
Contributors: Admin

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth
sequence.
Note: The sequence of integers will be represented as a string.

Hide Company Tags
Facebook
Hide Tags
String
Hide Similar Problems
(M) Encode and Decode Strings

    /*
     1.     1
     2.     11
     3.     21
     4.     1211
     5.     111221 
     6.     312211
     7.     13112221
     8.     1113213211
     9.     31131211131221
     10.   13211311123113112211
     https://discuss.leetcode.com/topic/2309/show-an-answer-in-java
    */
    public String countAndSay(int n) {
        StringBuilder curr = new StringBuilder("1");
        StringBuilder prev;
        
        int count;
        char say;
        
        for (int i= 1; i < n; i++) {
            prev = curr;
            curr = new StringBuilder();
            count = 1;
            say = prev.charAt(0);
            
            for (int j = 1, len = prev.length(); j < len;j++) {
                if (prev.charAt(j) != say) {
                    curr.append(count).append(say);
                    count = 1;
                    say = prev.charAt(j);
                } else count++;
            }
            curr.append(count).append(say);
        }
        return curr.toString();
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容