Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fd602b

Browse files
add 38. count and say
1 parent db48016 commit 8fd602b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎py2/38. Count and Say.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def countAndSay(self, n):
3+
"""
4+
:type n: int
5+
:rtype: str
6+
"""
7+
if n == 1:
8+
return '1'
9+
else:
10+
seq = self.countAndSay(n-1)
11+
split_seq = self.split_seq(seq)
12+
return "".join([str(len(nums)) + str(nums[0]) for nums in split_seq])
13+
14+
15+
def split_seq(self, seq):
16+
ret = [[seq[0]], ]
17+
for num in seq[1:]:
18+
if num in ret[-1]:
19+
ret[-1].append(num)
20+
else:
21+
ret.append([num])
22+
return ret

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /