blob: f73670ee33835b2539059e1f952d2d6894bc45c6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 233. Number of Digit One
"""
given an integer 'n', count the total number of digit 1 appearing in all
non-negative integers less than or equal to n
"""
class Solution(object):
def countDigitOne(self, n):
"""
:type n: int
:rtype: int
"""
ones, m, r = 0, 1, 1
while n > 0:
ones += (n + 8) / 10 * m + (n % 10 == 1) * r
r += n % 10 * m
m *= 10
n /= 10
return ones
if __name__ == "__main__":
obj = Solution()
print(obj.countDigitOne(13)) # expect: 6
print(obj.countDigitOne(0)) # expect: 0
|