357. Count Numbers with Unique Digits

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])

Solution1:Backtracking

总结见:http://www.jianshu.com/p/883fdda93a66
思路:Backtracking基本套路
Time Complexity: O(10!) worst case, or O(n!) if n < 10.
Space Complexity: O(10)

Solution2:DP

思路:
f(1) = 10,
f(2) = 9 * 9,
f(3) = f(2) * 8,
f(4) = f(3) * 7,
...,
f(10) = f(9) * 1
f(11) = 0
result = sum of f(1...10)
Time Complexity: O(1) Space Complexity: O(1)

Solution1 Code:

public class Solution {
    public static int countNumbersWithUniqueDigits(int n) {
        if (n > 10) {
            return countNumbersWithUniqueDigits(10);
        }
        int count = 1; // x == 0
        long max = (long) Math.pow(10, n);

        boolean[] used = new boolean[10];

        // outerloop to avoid leading zero
        for (int i = 1; i < 10; i++) {
            used[i] = true;
            count += search(i, max, used);
            used[i] = false;
        }

        return count;
    }

    private static int search(long prev, long max, boolean[] used) {
        int count = 0;
        if (prev < max) {
            count += 1;
        } else {
            return count;
        }

        for (int i = 0; i < 10; i++) {
            if (!used[i]) {
                used[i] = true;
                long cur = 10 * prev + i;
                count += search(cur, max, used);
                used[i] = false;
            }
        }

        return count;
    }
}

Solution2 Code:

class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if (n == 0) return 1;
        
        int res = 10;
        int uniqueDigits = 9;
        int availableNumber = 9;
        while (n-- > 1 && availableNumber > 0) {
            uniqueDigits = uniqueDigits * availableNumber;
            res += uniqueDigits;
            availableNumber--;
        }
        return res;
    }
}

你可能感兴趣的:(357. Count Numbers with Unique Digits)