Skip to content

Commit bfa9662

Browse files
committed
报数
1 parent f64884a commit bfa9662

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

count_and_say.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)