Count Numbers with Unique Digits——Difficulty:Medium
2016-11-14 15:22
337 查看
Problem :
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])
**
Algorithm:
**可以把n看成位数,设F(k)表示k位数中有多少个数是符合条件的,那么我们要求得结果就变成了F(k)+F(k-1)+F(k-2)+……+F(1),而F(k)=9*9*8*……(9-k+2)
**
Code:
class Solution { public: int countNumbersWithUniqueDigits(int n) { if(n==0) return 1; if(n==1) return 10; int a=10; for(int j=2;j<=n;j++) { int sum=9; for(int i=2;i<=j;i++) { sum*=11-i; } a+=sum; } return a; } };
相关文章推荐
- (算法分析Week12)Count Numbers with Unique Digits[Medium]
- LeetCode #357 - Count Numbers with Unique Digits - Medium
- LeetCode #357 - Count Numbers with Unique Digits - Medium
- Java [Leetcode 357]Count Numbers with Unique Digits
- Count Numbers with Unique Digits
- leetcode -- Count Numbers with Unique Digits
- LeetCode[357] Count Numbers with Unique Digits
- Leetcode 357 Count Numbers with Unique Digits
- 357.[LeetCode]Count Numbers with Unique Digits
- Leetcode357: Count Numbers with Unique Digits
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
- leetcode 之 Count Numbers with Unique Digits
- LeetCode 357 Count Numbers with Unique Digits
- 357.leetcode Count Numbers with Unique Digits(easy)[数学问题 非重复数字]
- [357]Count Numbers with Unique Digits
- [LeetCode]Count Numbers with Unique Digits
- leetcode Count Numbers with Unique Digits
- [leetcode-357]Count Numbers with Unique Digits(java)
- Count Numbers with Unique Digits
- LeetCode 357: Count Numbers with Unique Digits