forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_and_say.py
More file actions
29 lines (27 loc) · 734 Bytes
/
count_and_say.py
File metadata and controls
29 lines (27 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
class Solution:
# @param {int} n the nth
# @return {string} the nth sequence
def countAndSay(self, n):
# Write your code here
return self._countAndSay(n)
def _countAndSay(self, n):
if n == 1:
return '1'
ret = ''
prev_str = self._countAndSay(n - 1)
i = 0
while i < len(prev_str):
ch = prev_str[i]
sum = 1
j = i + 1
while j < len(prev_str):
if prev_str[i] == prev_str[j]:
sum += 1
j += 1
else:
break
ret += str(sum)
ret += ch
i = j
return ret