本文主要是介绍LeetCode-Count Numbers with Unique Digits,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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,99
Constraints:
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;}
这篇关于LeetCode-Count Numbers with Unique Digits的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!