• 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
  • 相关阅读:
    jenkins+svn+Ant+tomcat+非maven项目构建
    阿里云服务器CentOS6.9安装jenkins
    jenkins构建项目时报错缺少com.sun.image.codec.jpeg包解决方案
    不间断会话服务:screen
    数据库备份与还原:mysqldump,source
    多表更新:update,join
    删除数据高级用法:delete,truncate
    远程传输文件命令:scp
    远程控制服务:配置sshd服务,密钥验证登陆
    绑定多块网卡:负载均衡
  • 原文地址:https://www.cnblogs.com/catyuang/p/11134786.html
Copyright © 2020-2023  润新知