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 is the total numbers in the range of 0≤x <, excluding [11,22,33,44,55,66,77,88,99]
)
Hint:
- A Direct backtracking approach.
- Backtracking should contains three states which is (the current number, number of steps to get, and a bitmask which represent which number is marked as visited so far in the current number). Start with State (0,0,0) and count all valid number till we reach number of steps equals to 10n.
- This problem can also is solved using a dynamic programming approach and some knowledge of combinatorics.
- Let f (k) = Count of numbers with the unique digits with length equals K.
- F (1) = ten, ..., f (k) = 9 * 9 * 8 * ... (9-k + 2) [The first factor is 9 because a number cannot start with 0].
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
This problem let us find a range of people on the different numbers, such as 123 is not the same number of people, and 11,121,222 is not such a number. Then we can know according to the last article of the hint, the number of a single number satisfies the requirement is 10 (0 to 9), two digit satisfies test instructions is 81, [10-99] These 90 numbers remove [11,22,33,44,55,66,77,88,99] This 9 number, still has 81. The general formula is f (k) = 9 * 9 * 8 * ... (9-k + 2), then we can according to the size of N, the [1, n] interval digits by the general formula to calculate the sum up, see the code as follows:
Solution One:
classSolution { Public: intCountnumberswithuniquedigits (intN) {if(n = =0)return 1; intres =0; for(inti =1; I <= N; ++i) {Res+=count (i); } returnRes; } intCountintk) {if(K <1)return 0; if(k = =1)return Ten; intres =1; for(inti =9; I >= ( One-K); --i) {res*=i; } returnRes *9; }};
The following method is the lite version of the method above, and the idea is exactly the same:
Solution Two:
classSolution { Public: intCountnumberswithuniquedigits (intN) {if(n = =0)return 1; intres =Ten, cnt =9; for(inti =2; I <= N; ++i) {cnt*= ( One-i); Res+=CNT; } returnRes; }};
Finally, we take a look at the topic of the method of backtracking, we need a variable used, the second binary I bit 1 indicates that the number I appeared, just beginning we traverse 1 to 9, for each traversed to the number, now used in the tag has occurred, and then call the recursive function. In a recursive function, if the number is less than the maximum value, the result res is increased by 1, otherwise the RES is returned. Then traverse 0 to 9, if the current number does not appear in the used, at this point in the used, and then the current number is multiplied by 10 plus I, and then continue to call the recursive function, so that we can traverse to all cases, see the code is as follows:
Solution Three:
classSolution { Public: intCountnumberswithuniquedigits (intN) {intres =1, max = POW (Ten, n), used =0; for(inti =1; I <Ten; ++i) {used|= (1<<i); Res+=Search (i, max, used); Used&= ~ (1<<i); } returnRes; } intSearchintPreintMaxintused) { intres =0; if(Pre < MAX) + +Res; Else returnRes; for(inti =0; I <Ten; ++i) {if(! (Used & (1<<i)) {used|= (1<<i); intCur =Ten* PRE +i; Res+=Search (cur, max, used); Used&= ~ (1<<i); } } returnRes; }};
Resources:
Https://leetcode.com/discuss/107981/backtracking-solution
Https://leetcode.com/discuss/108119/java-concise-dp-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Count Numbers with Unique Digits calculates the number of different numbers for each of you