標籤:出現 while 等於 數字 getting obj recursion 今天 block
今天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.results = Solution()print(s.countDigitOne(11221))
maximum recursion depth exceeded while getting the str of an object
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.results = Solution()for i in range(0,1000000): print(i) print(s.countDigitOne(i))
輸出 998,然後報錯,最大遞迴深度找到了,還是安心用while吧~
關於python最大遞迴深度 - 998