Digital is different? Arrange on a combination of arrangements!
The meaning of problems as follows:
Given a non-negative integer n, the number of digits of calculation are different numbers x, where 0 ≤ x <10 ^ n.
Example:
Input: 2
Output: 91
Explanation: 11,22,33,44,55,66,77,88,99 answer should be removed, all numbers in [0,100) interval.
Analysis topic
For the first chestnuts
- n = 1
i.e., x ∈ [0, 10)
X = 0. 1. 5. 4. 3 2. 6. 7. 8. 9 (a total of 10 selection)
(a number of the same number appears certainly not matter) - n = 2
i.e., x ∈ [0, 100)
X isDouble-digit_ _
If the first bit to take 1 ==== second may take2 3 4 5 6 7 8 9 0(A total of nine selected)
first can take1 2 3 4 5 6 7 8 9(Note that can not take a total of nine select 0)
number = 9 * 9 =81
---- Total 10 + 81 = 91 - n = 3
i.e., x ∈ [0, 1000)
X isThree digits_ _
If the first bit to take 1 ==== second may take2 3 4 5 6 7 8 9 0(Total of 9 selected)
, if the second take 2 ==== third can take3 4 5 6 7 8 9 0(A total of eight select)
the first can take1 2 3 4 5 6 7 8 9(Note that can not take a total of nine select 0)
number = 8 * 9 * 9 =648
---- Total 10 + 81 + 648 = 739
...
so
finally when the number of bits of xMore than 10The time will certainlyDigital duplication
So long as the discussion of 10 kinds of circumstances
because A main lazy ... so ... violent Dafa is good! (# ^. ^ #)
code show as below:
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n==0) return 1;
if (n == 1) return 10;
else if (n == 2) return 91;
else if (n == 3) return 9*9*8 + 91;
else if (n == 4) return 9*9*8*7 + 9*9*8 + 91;
else if (n == 5) return 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
else if (n == 6) return 9*9*8*7*6*5 + 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
else if (n == 7) return 9*9*8*7*6*5*4 + 9*9*8*7*6*5 + 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
else if (n == 8) return 9*9*8*7*6*5*4*3 + 9*9*8*7*6*5*4 + 9*9*8*7*6*5 + 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
else if (n == 8) return 9*9*8*7*6*5*4*3*2 + 9*9*8*7*6*5*4*3 + 9*9*8*7*6*5*4 + 9*9*8*7*6*5 + 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
else return 9*9*8*7*6*5*4*3*2*1 + 9*9*8*7*6*5*4*3*2 + 9*9*8*7*6*5*4*3 + 9*9*8*7*6*5*4 + 9*9*8*7*6*5 + 9*9*8*7*6 + 9*9*8*7 + 9*9*8 + 91;
}
};