-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path357_CountNumbersWithUniqueDigits357.java
70 lines (61 loc) · 1.74 KB
/
357_CountNumbersWithUniqueDigits357.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* 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])
*/
public class CountNumbersWithUniqueDigits357 {
public int countNumbersWithUniqueDigits(int n) {
n = Math.min(n, 10);
int res = 1;
for (int i=1; i<=n; i++) {
res += count(i);
}
return res;
}
private int count(int i) {
int res = 9;
int k = 1;
while (k < i) {
res *= (10-k);
k++;
}
return res;
}
/**
* https://leetcode.com/problems/count-numbers-with-unique-digits/discuss/83037/Very-simple-15-line-backtrack-solution
*/
public int countNumbersWithUniqueDigits2(int n) {
return count(n, new boolean[10], 0);
}
private int count(int n, boolean[] used, int l) {
if (l == n) return 1;
int res = 0;
for (int i = (l == 0 ? 1 : 0); i<=9; i++) {
if (!used[i]) {
used[i] = true;
res += count(n, used, l+1);
used[i] = false;
}
}
return res;
}
public int countNumbersWithUniqueDigits3(int n) {
int[] res = new int[1];
count(n, new boolean[10], 0, res);
return res[0];
}
private void count(int n, boolean[] used, int l, int[] res) {
res[0]++;
if (l == n) return;
for (int i = (l == 0 ? 1 : 0); i<=9; i++) {
if (!used[i]) {
used[i] = true;
count(n, used, l+1, res);
used[i] = false;
}
}
}
}