python算法-1字符串-10数数Leetcode 038 Count and Say

python算法-1字符串-10数数Leetcode 038 Count and Say

 

在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_038_Count_and_Say.html

 

def countAndSay(n):
    if n == 0:
        return ''
    res = '3'
    while n != 0:
        n -= 1
        i = 0
        count = 1
        cur = ''
        # 拼接res的读数
        while i < len(res):
            count = 1
            # 有下一个,且下一个字母相等
            while i + 1 < len(res) and res[i] == res[i + 1]:
                count += 1
                i += 1
            # 数量+本身
            cur += str(count) + res[i]
            # 到下一个数字位
            i += 1
        res = cur
        print(res)
    return res


s = countAndSay(6)

 

你可能感兴趣的:(算法LeetCode)