forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountNumbersWithUniqueDigits.cpp
70 lines (61 loc) · 1.92 KB
/
CountNumbersWithUniqueDigits.cpp
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
// Source : https://leetcode.com/problems/count-numbers-with-unique-digits/
// Author : Hao Chen
// Date : 2019-03-24
/*****************************************************************************************************
*
* Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
*
* Example:
*
* Input: 2
* Output: 91
* Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
* excluding 11,22,33,44,55,66,77,88,99
*
******************************************************************************************************/
// Considering three digits
// - the first place could be [1-9] which has 9 choices.
// - the second place could be [0-9] with excluding the first digit, which is 10-1=9 choices.
// - the third place could be [0-9] with excluding the 1st and 2nd digits, which is 10-2=8 choices.
// So, three digits has 9*9*8 unique digits.
//
// After adds the 1 digit unique number,and 2 digits unique number, we can have the result:
//
// 9*9*8 + 9*9 + 10 = 648 + 81 + 10 = 739
//
// n = 0, a[0] = 1;
// n = 1, a[1] = 9 + a[0];
// n = 2, a[2] = 9*9 + a[1];
// n = 3, a[3] = 9*9*8 + a[2];
// n = 4, a[4] = 9*9*8*7 + a[3];
// ....
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int result = 1;
for (int i=0; i<n; i++) {
result += ( 9 * nine_factor(i) );
}
return result;
}
int nine_factor(int n) {
int result = 1;
for (int i=0; i<n; i++) {
result *= (9-i);
}
return result;
}
};
//actually, the function nine_factor() could be optimized!
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int result = 1;
int nine_factor = 1;
for (int i=0; i<n; i++) {
result += ( 9 * nine_factor );
nine_factor *= (9-i);
}
return result;
}
};