357. Count Numbers with Unique Digits

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

给出一个非负整数n,计算出所有没有重复数字的x,其中0≤x<10n

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

设k为整数的位数,f(k)表示k位数中符合要求的数的个数:

f(1)=10;

f(2)=9*9;

f(3)=9*9*3;

……;

f(k)=9*9*...*(10-k+1);

ret=f(1)+f(2)+...+f(n);

 1 class Solution {
 2 public:
 3     int countNumbersWithUniqueDigits(int n) {
 4         int ret=1,s=9;
 5         for(int i=1;i<=n;i++){
 6             if(i==1){ret=ret+s;continue;}
 7             s=s*(10-i+1);
 8             ret=ret+s;
 9         }
10         return ret;
11     }
12 };
原文地址:https://www.cnblogs.com/Z-Sky/p/5652979.html