您的位置:首页 > 其它

leetcode_Count and Say

2017-05-18 20:33 429 查看
Problem description:

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.
My solution(python):
class Solution(object):
def countAndSay(self, n):
i = 4
s = str(1)
slist = ['1', '11', '21']
for i in range(i, n+1):
scount = 0 # 记录i+1中字符串的每个位数
count = 0 # 记录字符串中每组相同数字的个数
s0 = slist[i-2] # s0代表之前的字符串
s = '' # s 代表生成新的kong字符串
k = 0
for k in range(k, len(s0)):
if k == len(s0)-1:
count += 0
s = s + str(count + 1) + s0[k]
slist.append(s)
elif k < len(s0)-1 and s0[k] == s0[k+1]:
count += 1
else:
s = s + str(count + 1) + s0[k]
count = 0
return slist[n-1]
"""
:type n: int
:rtype: str
"""

I used two layers of circulation.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: