-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38.py
34 lines (31 loc) · 886 Bytes
/
38.py
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
30
31
32
33
34
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n==1:
return str(1)
bef = ['1']
for i in range(1,n):
if len(bef)==1:
bef = [str(1), bef[0]]
else:
x = bef[0]
count = 1
now = []
for j in range(1, len(bef)):
if bef[j]==x:
count += 1
else:
now.append(str(count))
now.append(x)
x = bef[j]
count = 1
now.append(str(count))
now.append(x)
bef = now
return str(''.join(bef))
if __name__ == '__main__':
solution = Solution()
print solution.countAndSay(1)