LeetCode 0357 - Count Numbers with Unique Digits

2021-08-11 11:31:52 浏览数 (1)

Count Numbers with Unique Digits

Desicription

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:

代码语言:javascript复制
Input: 2
Output: 91 
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, 
             excluding 11,22,33,44,55,66,77,88,99

Solution

代码语言:javascript复制
class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
        if(n == 0) {
            return 1;
        }

        int res = 10;
        int count = 9;
        for(int i = 2; i <= n; i  ) {
            count *= (11 - i);
            res  = count;
        }

        return res;
    }
};

0 人点赞