357. Count Numbers with Unique Digits!

class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if (n==0) return 1;
        // initialize when n==1;
        int ans = 10;
        // the number of unique digits number of current length (1)
        // i.e 1,2,...9;
        int base = 9;
        for (int i=2; i<=n; i++){
            base*=10-(i-1);
            ans +=base;
        }
        return ans;
    }
}

Last updated