• 为了保证你在浏览本网站时有着更好的体验，建议使用类似Chrome、Firefox之类的浏览器~~
• 如果你喜欢本站的内容何不Ctrl+D收藏一下呢，与大家一起分享各种编程知识~
• 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此，生命在于折腾，何不年轻时多折腾一下

# 38. Count and Say

2年前 (2019-02-13) 675次浏览
The count-and-say sequence is the sequence of integers with the first five terms as following:
1.     1
2.     11
3.     21
4.     1211
5.     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 where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"


Example 2:

Input: 4
Output: "1211"

class Solution:
def countAndSay(self, n) :

if n==1:
return '1'
elif n==2:
return '11'
data=self.countAndSay(n-1)
count=dict()
result=''
tmp_char=''
for x in range(len(data)):
if not tmp_char:
tmp_char=data[x]
count[data[x]]=1
continue
if data[x]==tmp_char:
count[data[x]]+=1
else:
result+=str(count[tmp_char])+tmp_char
tmp_char=data[x]
count[data[x]]=1
if x==len(data)-1 :
result+=str(count[tmp_char])+tmp_char

return result



Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明38. Count and Say

[@lc@]  