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为种子,生成一个符合题意的结果即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
string = '1'
for i in range(n-1):
length = len(string)
count = 1
temp = ''
for m in range(length):
if (m == length-1):
temp += str(count)
temp += string[m]
else:
if (string[m] == string[m+1]):
count += 1
else:
temp += str(count)
temp += string[m]
count = 1
string = temp
return string