-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38. Count and Say.py
53 lines (46 loc) · 1.34 KB
/
38. Count and Say.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# @Time : 2019/3/2 14:34
# @Author : xulzee
# @Email : [email protected]
# @File : 38. Count and Say.py
# @Software: PyCharm
class Solution:
def countAndSay1(self, n: int) -> str:
if n == 1:
return "1"
result = self.countAndSay(n - 1)
i = result[0]
count_i = 1
temp = ""
for j in range(1, len(result)):
if result[j] == i:
count_i += 1
else:
temp = temp + str(count_i) + str(i)
count_i = 1
i = result[j]
temp = temp + str(count_i) + str(i)
return temp
def countAndSay(self, n: int) -> str:
if n == 1:
return "1"
if n == 2:
return "11"
pre = "11"
for i in range(3,n+1):
temp = ""
count = 1
count_char = pre[0]
for j in range(1, len(pre)):
if count_char == pre[j]:
count += 1
else:
temp = temp + str(count) + str(count_char)
count_char = pre[j]
count = 1
temp = temp + str(count) + str(count_char)
pre = temp
return pre
if __name__ == '__main__':
for n in range(1, 21):
print(Solution().countAndSay(n))