本文共 671 字,大约阅读时间需要 2 分钟。
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,99Constraints:
0 <= n <= 8
分析
F(0) = 1 F(1) = 10 F(2) = 10 +9(第一个数不能为0)9(可以从0~9中选出一个任意一个不与第一位相同的数) F(3) = 10 +99 + 998 F(4) = …F(n) = F(n-1) + 9 * 8 * …* 9-n+1;
public int countNumbersWithUniqueDigits(int n) { if(n == 0) return 1; int sum = 10; int step = 9; int K = 9; for( int i = 0 ; i < n-1;i ++){ step = step *(K -i); sum += step; } return sum; }
转载地址:http://cvwxi.baihongyu.com/