p357

LeetCode 357 Count Numbers with Unique Digits 题解

1.题目:

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 ≤ x < 10n.每个位置上数字都不相同的x的个数

2.解题思路:

排序取中间

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 

public class Solution {
public int countNumbersWithUniqueDigits(int n) {
int ans=9;
int j=9;
if (n==1)
{
return 10;
}
if (n==0)
{
return 1;
}
if (n>10)
{
return 8877691;
}
for (int i=n;i>1;i--)
{
ans=ans*j;
j--;
}
return ans+countNumbersWithUniqueDigits(n-1);

}
}


4.一些总结: