leetcode 每日一题 38. 外观数列

双指针

思路:

一个指针负责遍历,另一个指针负责记录,遇到不同值时更新状态。

class Solution:
    def countAndSay(self, n: int) -> str:
        def getResult(para:str)->str:
            record = para[len(para)-1]
            result = "" 
            n = 0
            for i in para[::-1]:
                if i == record:
                    n += 1
                else:
                    result = result + record + str(n)
                    record = i
                    n = 1
            else:
                result = result + record + str(n)
            return result[::-1]
        res = "1"
        for i in range(1,n):
            res = getResult(res)
        return res

 

posted @ 2020-05-20 13:15  nil_f  阅读(142)  评论(0编辑  收藏  举报