-
-
Save cpylua/2050431 to your computer and use it in GitHub Desktop.
count total number of ones in 1...n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(n): | |
cache = [0] | |
cnt = 0 | |
for i in xrange(1, n+1): | |
remainder = i % 10 | |
quotient = i / 10 | |
m = cache[quotient] | |
if remainder == 1: | |
m = m + 1 | |
cache.append(m) | |
cnt = cnt + m | |
return cnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment