38. Count and Say
1. 1
2. 11
3. 21
4. 1211
5. 111221Input: 1
Output: "1"Input: 4
Output: "1211"class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n < 1: return '0'
res = '1'
for k in xrange(1,n):
i, j, tmp = 0,0, ''
while j < len(res):
if res[i] == res[j]:
j +=1
else:
tmp += str(j - i) + str(res[i])
i = j
tmp += str(j - i) + str(res[i])
res = tmp
return resLast updated
Was this helpful?