zoukankan      html  css  js  c++  java
  • leetcode38. 报数 🌟

    题目:

      报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:

        1. 1
        2. 11
        3. 21
        4. 1211
        5. 111221
       1 被读作  "one 1"  ("一个一") , 即 11。
       11 被读作 "two 1s" ("两个一"), 即 21。
       21 被读作 "one 2",  "one 1" ("一个二" ,  "一个一") , 即 1211。

      给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。

      注意:整数顺序将表示为一个字符串。

    示例 1:

      输入: 1
      输出: "1"
    示例 2:

      输入: 4
      输出: "1211"

    来源:力扣(LeetCode)

    解答:

    leetcode优秀方案(来自力扣答案统计页,没有明确作者是谁,可留言告知):

    class Solution:
        def countAndSay(self, n: int) -> str:
    
            if n == 1: return '1'  # 递归出口
            s = self.countAndSay(n-1)
            res, count = '', 0
            for i in range(len(s)):
                count += 1
                if i == len(s) - 1 or s[i] != s[i+1]:  # 最后一个字符串要提前终止
                    res += str(count)
                    res += s[i]
                    count = 0
            return res
    View Code
    class Solution:
        def countAndSay(self, n: int) -> str:
            def recursion(s, n):
                if n == 1:
                    return s
                else:
                    key = s[0]
                    s_next = ""
                    count = 0
                    for i in range(len(s)):
                        if s[i] == key:
                            count += 1
                        else:
                            s_next += str(count) + key
                            count = 1
                            key = s[i]
                    return recursion(s_next + str(count) + key, n - 1)
    
            return recursion('1', n)
    View Code
    class Solution:
        def countAndSay(self, n: int) -> str:
            result = '1'
            
            for loop in range(n-1):
                i = 0
                temp = ''
                while i < len(result):
                    cur_count = 1
                    while i<len(result)-1 and result[i]==result[i+1]:
                        cur_count += 1
                        i += 1
                    temp = temp + str(cur_count) + result[i]
                    i += 1
                result = temp
            return result
    View Code
    class Solution:
        def countAndSay(self, n: int) -> str:
            def worker(n):
                n = str(n)
                i = 0
                j = k = 1
                temp = []
                while i <= len(n) - 1 and j <= len(n):
                    if j == len(n):
                        temp.append((str(k), n[-1]))
                        return ''.join([j for i in temp for j in i])
    
                    if n[i] == n[j]:
                        j += 1
                        k += 1
                    else:
                        temp.append((str(k), n[i]))
                        i = j
                        j = i + 1
                        k = 1
    
                return ''.join([j for i in temp for j in i])
    
            _cache = {1: '1'}
            for i in range(1, n + 1):
                if i not in _cache:
                    _cache[i] = worker(_cache[i - 1])
    
            return _cache[n]
    View Code
    class Solution:
        def countAndSay(self, n: int) -> str:
            if n==1:
                return "1"
            else:
                import re
                str1=""
                pattern=re.compile(r'(d)1{0,}')
                for i in pattern.finditer(self.countAndSay(n-1)):
                    str1 += str(len(i.group(0))) + i.group(0)[0]
                return str1
    # https://leetcode-cn.com/problems/count-and-say/comments/2777
    View Code
    class Solution:
        def countAndSay(self, n):
            import re
            s = '1'
            for _ in range(n - 1):
                s = ''.join(str(len(p[0])) + p[1] for p in re.findall(r'((.)2*)', s))
            return s
    # https://leetcode-cn.com/problems/count-and-say/comments/2915
    View Code
  • 相关阅读:
    ASCII、Unicode和UTF-8等常见字符编码格式介绍
    pycharm创建脚本头文件模板
    pycharm常用设置项和快捷键
    Genymotion安装apk问题
    [Android测试] Appium的一些坑问题错误解决 与 技巧集锦
    Appium+python自动化测试过程中问题
    python客户端和Appium服务端联调出现的问题解决办法
    移动端自动化测试环境搭建
    "http://127.0.0.1:4723/wd/hub"的解释
    wireshark抓包看ECN
  • 原文地址:https://www.cnblogs.com/catyuang/p/11134786.html
Copyright © 2011-2022 走看看