class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int ret=1;
int product=1;
for(int i=0,j=9;i<n;i++)
{
ret+=product*9;
product*=j--;
}
return ret;
}
};
/*
public class Solution {
public static int countNumbersWithUniqueDigits(int n) {
if (n > 10) {
return countNumbersWithUniqueDigits(10);
}
int count = 1; // x == 0
long max = (long) Math.pow(10, n);
boolean[] used = new boolean[10];
for (int i = 1; i < 10; i++) {
used[i] = true;
count += search(i, max, used);
used[i] = false;
}
return count;
}
private static int search(long prev, long max, boolean[] used) {
int count = 0;
if (prev < max) {
count += 1;
} else {
return count;
}
for (int i = 0; i < 10; i++) {
if (!used[i]) {
used[i] = true;
long cur = 10 * prev + i;
count += search(cur, max, used);
used[i] = false;
}
}
return count;
}
}
*/
/*
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
vector<int> tbl(min(n,10)+1, 1);
for (int i = 1; i <= min(n,10); i++)
tbl[i] = tbl[i-1] * (i == 1? 9: (9-i+2));
return accumulate(tbl.begin(), tbl.end(), 0);
}
};
*/
357. Count Numbers with Unique Digits
最新推荐文章于 2020-06-05 12:35:22 发布