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.
解法1: Recursive
用递归求得n-1的字符串,然后一位位遍历,每当得到和前一位不一样的字符时,保存前一位的字符的计数和字符。要注意的是最后一位的结果要在扫描结束后保存。
C++C++ 里 从1个char转换到string可以用string(1, character)
Java