- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
357. Count Numbers with Unique Digits
        Jacky Zhang edited this page Aug 24, 2016 
        ·
        1 revision
      
    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
解题思路为Dynamic Programming。 令 f(k) = count of numbers with unique digits with length equals k, 则f(1) = 10, f(2) = 9*9, f(3) = 9*9*8, ... f(k) = 9*9*8*...(11-k) 将他们求和即是所求。
public class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if(n < 1) return 1;
        int count = 10;
        int prevCount = 9;
        for(int i = 2; i <= n; i++) {
            int currCount = prevCount * (11 - i);
            count += currCount;
            prevCount = currCount;
        }
        return count;
    }
}