weqopy
9/15/2017 - 5:38 AM

digitCounts.py

class Solution:
    # @param k & n  two integer
    # @return ans a integer

    def digitCounts(self, k, n):
        count = 0
        # n 的位数
        ln = len(str(n))
        for i in range(1, ln + 1):
            # k 为 0 时,首位可跳过
            # k 为 0 的处理待完善
            if k == 0:
                if n > 0 and n < 10:
                    count = 1
                if i == ln:
                    break
            a = n // (10 ** i) * (10 ** (i - 1))


            n_i = n % (10 ** i) // (10 ** (i - 1))
            if n_i > k:
                num = a + 10 ** (i - 1)
            elif n_i < k:
                num = a
            elif n_i == k:
                num = a + 1 + n % (n_i * (10 ** (i - 1)))
            count += num
        if n == 0:
            if k == 0:
                count = 1
            else:
                count = 0
        return count

lt = [0, 3, 12, 24, 35, 456, 9999]
ks = [0, 3, 9]

res = Solution()
for k in ks:
    for n in lt:
        print(res.digitCounts(k, n))