Created
August 13, 2018 10:25
-
-
Save luojiyin1987/ac0efa59d8895c8d482737a490cd8fb6 to your computer and use it in GitHub Desktop.
Count and Say
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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