zoukankan      html  css  js  c++  java
  • 关于python最大递归深度

    今天LeetCode的时候暴力求解233

    • 问题:

    给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数。

    例如:

    给定 n = 13,

    返回 6,因为数字1出现在下数中出现:1,10,11,12,13。

    • 代码:
    class Solution:
        def __init__(self):
            self.key = '1'
            self.result = 0
    
        def countDigitOne(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n < 1:
                return self.result
            self.result += str(n).count(self.key)
            if n > 0:
                self.countDigitOne(n-1)
            return self.result
    
    s = Solution()
    print(s.countDigitOne(11221))
    
    • 错误:

    maximum recursion depth exceeded while getting the str of an object

    • 寻找python最大递归深度
    class Solution:
        def __init__(self):
            self.key = '1'
            self.result = 0
    
        def countDigitOne(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n < 1:
                return self.result
            self.result += str(n).count(self.key)
            if n > 0:
                self.countDigitOne(n-1)
            return self.result
    
    s = Solution()
    for i in range(0,1000000):
        print(i)
        print(s.countDigitOne(i))
    

    输出 998,然后报错,最大递归深度找到了,还是安心用while吧~

  • 相关阅读:
    系统运维易忘点总结之七
    SqlServer的排序规则
    Oracle数据库用户密码过期解决
    linux 查看即时网速 /流量的工具
    NFS部署过程
    NFS 共享存储
    Rsync服务的实战
    ncRsync服务
    架构名词
    状态码
  • 原文地址:https://www.cnblogs.com/bincoding/p/8972238.html
Copyright © 2011-2022 走看看