Today, Leetcode, the Brute force solution 233
Given an integer n, calculates the number of occurrences of the number 1 in all non-negative numbers less than or equal to N.
For example:
Given n = 13,
Returns 6 because the number 1 appears in the following number: 1,10,11,12,13.
classSolution:def __init__( Self): Self. Key= ' 1 ' Self. Result= 0 defCountdigitone ( Self, N):""": Type N:int: Rtype:int """ ifN< 1:return Self. Result Self. Result+= Str(n). Count ( Self. Key)ifN> 0: Self. Countdigitone (n-1)return Self. Results=Solution ()Print(S.countdigitone (11221))
Maximum recursion depth exceeded while getting the STR in an object
- Find Python maximum recursion depth
classSolution:def __init__( Self): Self. Key= ' 1 ' Self. Result= 0 defCountdigitone ( Self, N):""": Type N:int: Rtype:int """ ifN< 1:return Self. Result Self. Result+= Str(n). Count ( Self. Key)ifN> 0: Self. Countdigitone (n-1)return Self. Results=Solution () forIinch Range(0,1000000):Print(i)Print(S.countdigitone (i))
Output 998, then error, maximum recursion depth found, or peace of mind with while bar ~
About Python maximum recursion depth-998