classSolution: defcountNumbersWithUniqueDigits(self, n: int) -> int: if n == 0: return1 if n == 1: return10 res, cur = 10, 9 for i inrange(n - 1): cur *= 9 - i res += cur return res
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intcountNumbersWithUniqueDigits(int n){ if (n == 0) { return1; } if (n == 1) { return10; } int ans = 10, cur = 9; for (int i = 0; i < n - 1; ++i) { cur *= 9 - i; ans += cur; } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintcountNumbersWithUniqueDigits(int n) { if (n == 0) { return1; } if (n == 1) { return10; } intres=10, cur = 9; for (inti=0; i < n - 1; i++) { cur *= 9 - i; res += cur; } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolution { publicintCountNumbersWithUniqueDigits(int n) { if (n == 0) { return1; } if (n == 1) { return10; } int res = 10, cur = 9; for (int i = 0; i < n - 1; i++) { cur *= 9 - i; res += cur; } return res; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funccountNumbersWithUniqueDigits(n int)int { if n == 0 { return1 } if n == 1 { return10 } ans, cur := 10, 9 for i := 0; i < n-1; i++ { cur *= 9 - i ans += cur } return ans }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intcountNumbersWithUniqueDigits(int n) { if (n == 0) { return1; } if (n == 1) { return10; } int ans = 10, cur = 9; for (int i = 0; i < n - 1; ++i) { cur *= 9 - i; ans += cur; } return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var countNumbersWithUniqueDigits = function(n) { if (n === 0) { return1; } if (n === 1) { return10; } let res = 10, cur = 9; for (let i = 0; i < n - 1; i++) { cur *= 9 - i; res += cur; } return res; };