1 parent f64884a commit bfa9662Copy full SHA for bfa9662
1 file changed
count_and_say.py
@@ -0,0 +1,29 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param {int} n the nth
5
+ # @return {string} the nth sequence
6
+ def countAndSay(self, n):
7
+ # Write your code here
8
+ return self._countAndSay(n)
9
10
+ def _countAndSay(self, n):
11
+ if n == 1:
12
+ return '1'
13
+ ret = ''
14
+ prev_str = self._countAndSay(n - 1)
15
+ i = 0
16
+ while i < len(prev_str):
17
+ ch = prev_str[i]
18
+ sum = 1
19
+ j = i + 1
20
+ while j < len(prev_str):
21
+ if prev_str[i] == prev_str[j]:
22
+ sum += 1
23
+ j += 1
24
+ else:
25
+ break
26
+ ret += str(sum)
27
+ ret += ch
28
+ i = j
29
+ return ret
0 commit comments