题目描述:
Given a non-negative integer n, count all numbers with unique digits, x, where 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])
思路是:恰好n位数形成无重复数是9*9*8*7....所以求出n位数构成的数,加上countNumbersWithUniqueDigits(n-1)即可。
public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if(n==0)
return 1;
if(n==1)
return 10;
int num=9;
for(int i=0;i<n-1;i++){
num*=(9-i);
}
return num+=countNumbersWithUniqueDigits(n-1);
}
}
本文介绍了一种计算特定范围内所有具有唯一数字组合的整数数量的方法。通过递归方式,结合数学乘法原理,实现了高效求解算法。适用于竞赛编程及数学问题解决。
2204

被折叠的 条评论
为什么被折叠?



