Skip to content

Commit

Permalink
Create digit-count-in-range.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jun 2, 2019
1 parent 0febfd3 commit 0955cdb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Python/digit-count-in-range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Time: O(logn)
# Space: O(1)

class Solution(object):
def digitsCount(self, d, low, high):
"""
:type d: int
:type low: int
:type high: int
:rtype: int
"""
def digitsCount(n, k):
pivot, result = 1, 0
while n >= pivot:
result += (n//(10*pivot))*pivot + \
min(pivot, max(n%(10*pivot) - k*pivot + 1, 0))
if k == 0:
result -= pivot
pivot *= 10
return result+1

return digitsCount(high, d) - digitsCount(low-1, d)

0 comments on commit 0955cdb

Please sign in to comment.