countAndSayPlus
来源:互联网 发布:威尼斯 知乎 编辑:程序博客网 时间:2024/05/17 23:17
我今天根据我自己的想法运行了输出这个特定序列的代码,但是复杂度导致的运行时间让人崩溃,所以我自己根据答案中的一种实现了一下,可能跟答案是一模一样的,但是我跟着它的思路走了一遍,自己有敲了一遍。
不知道到哪天我才能在写while时不陷入死循环,哎,我觉得会有想放铳子的激情
n = 6s = ['1']result = '1'for i in range(n-1): tmp = [] start = 0 while start < len(s): count = 1 next = start + 1 while next < len(s) and s[next] == s[start]: next = next + 1 count = count + 1 tmp.append(str(count)) tmp.append(str(s[start])) start = next print(result) result = ''.join(tmp) s = tmpprint(result)