Leetcode brush question record-38. Appearance series

Insert picture description here
The task of this problem is essentially
for a string composed of numbers,
we divide the adjacent and equal sections, and then output two numbers for each section.

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


Published 59 original articles · Liked 14 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_41545780/article/details/105479864