标签:重复 tps his target code http pre nbsp blank
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]
)
思路:
思路:
排列组合题。
设i为长度为i的各个位置上数字互不相同的数。
需要注意的是,9- i + 2 >0 即 i < 11,也就是i最大为10,正好把每个数都用了一遍。
参考自:https://www.hrwhisper.me/leetcode-count-numbers-unique-digits/
int countNumbersWithUniqueDigits(int n) { if (n == 0)return 1; if (n == 1)return 10; vector<int>dp(n+1,9); n = min(n,10); int cnt = 10; for (int i = 2; i <= n;i++) { int j = 11 - i; dp[i] = dp[i - 1] * j; cnt += dp[i]; } return cnt; }
[leetcode-357-Count Numbers with Unique Digits]
标签:重复 tps his target code http pre nbsp blank
原文地址:http://www.cnblogs.com/hellowooorld/p/7069573.html