Similar idea, here is my lookup (not really) version:

class Solution { public int countNumbersWithUniqueDigits(int n) { int[] counts = new int[11]; if (n == 0) return 1; //tricky corner case counts[1] = 10; for (int i = 2; i < 11; i++) counts[i] = 9 * permute(9, i - 1); int res = 0; for (int i = 1; i <= n; i++) res += counts[i]; return res; } public int permute(int n, int k) { int res = 1; for (int i = 0; i < k; i++) res *= n--; return res; } }The method permute(n, k) computes P(n, k) as defined in combinatorics: number of ways to arrange k different numbers in n consecutive slots.