-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38.外观数列.py
57 lines (48 loc) · 1.36 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
#
# @lc app=leetcode.cn id=38 lang=python3
#
# [38] 外观数列
#
# @lc code=start
class Solution:
# def countAndSay(self, n: int) -> str:
# # 没想到什么好的思路,直接遍历吧
# s = "1"
# for i in range(n-1):
# #初始化相关变量
# char = s[0]
# count = 0
# tmp = ''
# for j in s:
# if char == j:
# count += 1
# else:
# tmp = tmp+ str(count)+char
# count = 1
# char = j
# # 别忘记了
# tmp = tmp+str(count)+char
# s = tmp
# return s
#递归(参考的,递归debug更好理解)
def countAndSay(self, n: int) -> str:
if n<=1: return "1"
num = self.countAndSay(n-1)+"*"
print(num)
temp = list(num)
count = 1
strBulider = ''
# print(len(temp))
for i in range(len(temp)-1):
if temp[i] == temp[i+1] :
count += 1
else:
if temp[i] != temp[i+1]:
strBulider += str(count) + temp[i]
count = 1
return strBulider
# @lc code=end
if __name__ == "__main__":
S= Solution()
print(S.countAndSay(4))