您的位置:首页 > 产品设计 > UI/UE

leetcode357: Count Numbers with Unique Digits

2016-11-14 18:39 375 查看
要求:

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=0的情况

n=1 return 10

n=2 return 9*9+10

n=3 return 9*9*8+9*9+10

...

public static int countNumbersWithUniqueDigits(int n) {
if (n == 0)
return 1;
int flag = n;
int sum = 0;
for (int i = 0; i < n - 1; i++) {
int count = 9;
int pr = 9;
while (count > (10 - flag))
pr = pr * count--;
flag--; // 控制参与乘积的数字个数
sum += pr;
}
return sum + 10;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: