[leetcode]-357. Count Numbers with Unique Digits(C语言)

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

Example:

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,9
int countNumbersWithUniqueDigits(int n) {
    if(n==0)
        return 1;
    if(n==1)
        return 10;
    int sum=10;
    int i,j,k,t;
    for(i=2;i<=n;i++)
    {
        k=9;
        t=9;
        for(j=1;j<i;j++)
        {
            k=k*t;
            t--;
        }
        sum+=k;
    }
    return sum;
}

猜你喜欢

转载自blog.csdn.net/shen_zhu/article/details/82751136