Count Numbers with Unique Digits

https://www.lintcode.com/problem/count-numbers-with-unique-digits/description

public class Solution {
    /**
     * @param n: a non-negative integer
     * @return: number of numbers with unique digits
     */
    public int countNumbersWithUniqueDigits(int n) {
        // Write your code here
        int result = 0;
        for (int i = 0; i <= n; i++) {
            if (i == 0) {
                result += 1;
            } else if (i == 1) {
                result += 9;
            } else if (i <= 10) {
                int temp = 1;
                for (int j = 1; j <= i; j++) {
                    if (j == 1) {
                        temp *= 9;
                    } else {
                        temp *= (10 - j + 1);
                    }
                }
                result += temp;
            }
        }
        return result;
    }
}

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