leetcode357: Count Numbers with Unique Digits
2016-11-14 18:39
375 查看
要求:
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
注意:判断n=0的情况
n=1 return 10
n=2 return 9*9+10
n=3 return 9*9*8+9*9+10
...
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])
注意:判断n=0的情况
n=1 return 10
n=2 return 9*9+10
n=3 return 9*9*8+9*9+10
...
public static int countNumbersWithUniqueDigits(int n) { if (n == 0) return 1; int flag = n; int sum = 0; for (int i = 0; i < n - 1; i++) { int count = 9; int pr = 9; while (count > (10 - flag)) pr = pr * count--; flag--; // 控制参与乘积的数字个数 sum += pr; } return sum + 10; }
相关文章推荐
- LeetCode 357: Count Numbers with Unique Digits
- Leetcode357: Count Numbers with Unique Digits
- LeetCode[357] Count Numbers with Unique Digits
- leetcode_357 Count Numbers with Unique Digits
- Leetcode 357 Count Numbers with Unique Digits
- [leetcode-357]Count Numbers with Unique Digits(java)
- Java [Leetcode 357]Count Numbers with Unique Digits
- LeetCode - 357 - Count Numbers with Unique Digits
- LeetCode 357 Count Numbers with Unique Digits
- leetcode:数学:Count Numbers with Unique Digits(357)
- leetcode 357 Count Numbers with Unique Digits C++
- 【LEETCODE】357- Count Numbers with Unique Digits [Python]
- leetcode 357 c++. Count Numbers with Unique Digits
- LeetCode 357 Count Numbers with Unique Digits (排列组合)
- LeetCode 357 Count Numbers with Unique Digits
- Leetcode-357- Count Numbers with Unique Digits C#
- 357.leetcode Count Numbers with Unique Digits(easy)[数学问题 非重复数字]
- [LeetCode] Count Numbers with Unique Digits 计算各位数值不同的数的个数
- leetcode_Count Numbers with Unique Digits
- Math-357-Count Numbers with Unique Digits