-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode387.py
More file actions
27 lines (25 loc) · 1009 Bytes
/
Leetcode387.py
File metadata and controls
27 lines (25 loc) · 1009 Bytes
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
class Solution:
def countAndSay(self, n: int) -> str:
pre = ''
cur = '1'
# 从第 2 项开始
for _ in range(1, n):
# 这里注意要将 cur 赋值给 pre
# 因为当前项,就是下一项的前一项。有点绕,尝试理解下
pre = cur
# 这里 cur 初始化为空,重新拼接
cur = ''
# 定义双指针 start,end
start = 0
end = 0
# 开始遍历前一项,开始描述
while end < len(pre):
# 统计重复元素的次数,出现不同元素时,停止
# 记录出现的次数,
while end < len(pre) and pre[start] == pre[end]:
end += 1
# 元素出现次数与元素进行拼接
cur += str(end-start) + pre[start]
# 这里更新 start,开始记录下一个元素
start = end
return cur