Leetcodeブラシ質問記録-38。外観シリーズ

ここに画像の説明を挿入
この問題のタスクは基本的
に数値で構成される文字列であり
、隣接するセクションと等しいセクションを分割し、セクションごとに2つの数値を出力します。1つは一致数、もう1つは一致の長さです。

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


元の記事を公開59件 高く評価 14件 訪問者20,000人以上

おすすめ

転載: blog.csdn.net/weixin_41545780/article/details/105479864