【leetcode】1012. Numbers With Repeated Digits

Topics are as follows:

Given a positive integer N, return the number of positive integers less than or equal to N that have at least 1 repeated digit.

Example 1:

Input: 20
Output: 1
Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11.

Example 2:

Input: 100
Output: 10
Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100.

Example 3:

Input: 1000
Output: 262

Note:

  1. 1 <= N <= 10^9

Problem-solving ideas: the subject requires the least total number of digits of a duplicate elements, we can first find the total number of elements in the figures are not repeated, and then subtracting the total number N can be. How to obtain the number of digital elements do not repeat it? Suppose Input = 53254, the total number of bits than when the condition is determined first digital Input

Median 1: a total of 9 (1,2,3 .... 9)

2 digits: the most significant bit is not 0, only nine kinds of optionally 1 to a second bit in only eight choices 1 to 9, but may be 0, 1 is selected from 9, total 9 *9,

A median of 3: a total of 9 * 9 * 8 

Median 4: it is clear that the law, a total of 9 * A (4-1,9) (A is arranged Mathematics)

The case when the number of bits equal to the Input will be some trouble. My approach is to first find the total number of 5XXXX this format, and then finds the total number 53XXX this format until the final total is determined.

5XXXX: second bit is 0, 1 or 2 can, in the remaining bits, in addition to the selection of only the first and second bits other than eight numbers for a total of 3 * A (3,8)

53XXX: third only 0 or 1, the remaining bits, only in addition to the first, second, seven in number other than three selection, a total of 2 * A (2,7)

A similar situation after the 532XX

code show as below:

class Solution(object):
    def numDupDigitsAtMostN(self, N):
        """
        :type N: int
        :rtype: int
        """
        def calcPerm(v1,v2):
            v = 1
            while v1 > 0:
                v * = v2
                v2 -= 1
                v1 -= 1
            return v
        ln = list(str(N))
        RES =. 1 IF len (List (STR (N))) == len (SET (List (STR (N)))) the else 0 # determines whether or not contain duplicate elements N
        dic_used = [int(ln[0])]
        for i in range(1,len(ln)):
            count = 0
            for j in range(0,int(ln[i])):
                if j not in dic_used:
                    count += 1
            res += count * calcPerm(len(ln) - i - 1, 10 - i - 1)
            if int(ln[i]) in dic_used:
                break
            dic_used.append(int(ln[i]))

        #
        for i in range(1,len(ln)+1):
            if i != len(ln):
                res += (9 * calcPerm(i-1,9))
            else:
                count = int(ln[0]) - 1
                res += (count * calcPerm(i - 1, 9))
        #
        return N - res
        

 

Guess you like

Origin www.cnblogs.com/seyjs/p/11841424.html