Last active
August 29, 2015 14:24
-
-
Save msg7086/4477fb824f1d7968178c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# @param {Integer} n | |
# @return {Integer} | |
def count_digit_one(n) | |
digit = 10 | |
sum = 0 | |
while n > 0 | |
base = 10 ** digit | |
msb = n / base | |
lsb = n % base | |
# 3[XXXX] (0-29999) | |
sum += msb * digit * base / 10 if n >= base | |
# [3]XXXX (10000-19999) | |
sum += base if msb >= 2 | |
# [1]XXXX (10000-1XXXX) | |
sum += lsb + 1 if msb == 1 | |
n = lsb | |
digit -= 1 | |
end | |
sum | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment