#38 外观数列解法
一.解题思路二.代码展示三.总结其他
一.解题思路
思路:这题用的是双指针来做的,我这个写法效率比较低,后面准备参考一下别的双指针解法和递归解法,更新一下题解。
注意:先读懂题目,没看懂的看一下力扣上这题的评论区。
二.代码展示
class Solution:
def countAndSay(self
, n
):
res
= "1"
for _
in range(n
-1):
s_index
= e_index
= 0
tmp
= ""
res_len
= len(res
)
while res_len
>= e_index
:
if res
[s_index
] == res
[e_index
:e_index
+1]:
e_index
+= 1
elif res
[e_index
:e_index
+1] != "":
tmp
+= str(e_index
- s_index
)+res
[s_index
]
s_index
= e_index
else:
tmp
+= str(e_index
-s_index
) + res
[s_index
]
break
res
= tmp
return res
三.总结其他
总结:这题提交的时候效率低的吓人(虽然很多很多提交是无意义的,但是还相对其他解法低效),这题花了点时间,把逻辑理清楚才行。
优化:后面准备参考一下别人的双指针和递归解法,然后回来在更新一下代码。
交流:如果有什么建议或者疑问可以在文章下面回复哈,期待交流。