描述
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
1, 11, 21, 1211, 111221, …
1 读作 “one 1” -> 11
11 读作 “two 1s” -> 21
21 读作 “one 2, then one 1” -> 1211
给定一个整数 n, 返回 第 n 个顺序。
样例
样例 1:
输入:1
输出:“1”
样例 2:
输入:5
输出:“111221”
class Solution:
"""
@param n: the nth
@return: the nth sequence
"""
def countAndSay(self, n):
# write your code here
s = "1"
for i in range(1,n):
s = self.readString(s)
return s
def readString(self, s):
count = 0
string = ""
for i in range(len(s)):
if i == 0 or s[i] == s[i-1]:
count += 1
else:
string += str(count) + "" + s[i-1]
count = 1
string += str(count) + "" + s[len(s)-1]
return string