Задача этой задачи по существу
для строки, состоящей из чисел,
мы делим соседние и равные секции, а затем выводим два числа для каждой секции, одна - это совпадающее число, а другая - длина совпадения.
class Solution:
def countAndSay(self, n: int) -> str:
#根本任务:将上一个字符串分割
#分割标准为 相邻值若相等 则分到一组
newdict = {
1:'1',
2:'11',
3:'21',
4:'1211',
5:'111221'
}
if n <= 5:
return newdict[n]
i = 5
last = newdict[5]
while i < n:
last = self.each(last)
i += 1
return last
def each(self,inputstr):
res = ''
isvoid = True
times = 0#1
value = 0#inputstr[0]
length = len(inputstr)
#tempres = 0#inputstr[0]
for i in range(length):
if isvoid:
times = 1
value = inputstr[i]
isvoid = False
elif inputstr[i] == value:
times += 1
elif inputstr[i] != value:
res += str(times)
res += str(value)
value = inputstr[i]
times = 1
res += str(times)
res += str(value)
return res