python leetcode38 递归

来源:互联网 发布:爷爷9岁被鬼子杀了知乎 编辑:程序博客网 时间:2024/06/05 12:12

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.

class Solution(object):
def countAndSay(self,n):
if n == 1:
return “1”
else:
b = self.countAndSay(n-1)
return self.count(b)
def count(self,b):
a = “”
c = 0
i = 0
while (i < len(b)):
eql = b[i]
while b[i] == eql:
c += 1
i += 1
if i == len(b):
break
a += (str(c) + b[i - 1])
c = 0
return a

0 0
原创粉丝点击