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吧~

  • 相关阅读:
    神经网络加速与模型压缩
    字符串分割函数
    物体检测整理
    deeplab算法解析
    kmeans算法
    封装caffe版的deeplab为库供第三方使用
    进程 守护
    socketserver tcp黏包
    计算机
    网络 协议 2
  • 原文地址:https://www.cnblogs.com/bincoding/p/8972238.html
Copyright © 2011-2022 走看看