Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 13, 2018 10:25
Show Gist options
  • Save luojiyin1987/ac0efa59d8895c8d482737a490cd8fb6 to your computer and use it in GitHub Desktop.
Save luojiyin1987/ac0efa59d8895c8d482737a490cd8fb6 to your computer and use it in GitHub Desktop.
Count and Say
class Solution:
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
s = '1'
for i in range(1, n):
s = self.cal(s)
return s
def cal(self, s):
cnt = 1
length = len(s)
ans = ''
for i, c in enumerate(s):
if i+1 < length and s[i] != s[i+1]:
ans = ans + str(cnt) + c
cnt = 1
elif i+1 < length:
cnt = cnt + 1
ans =ans + str(cnt) + c
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment