357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
Posted lina2014
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了357 Count Numbers with Unique Digits 计算各个位数不同的数字个数相关的知识,希望对你有一定的参考价值。
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n。
示例:
给定 n = 2,返回 91。(答案应该是除[11,22,33,44,55,66,77,88,99]外,0 ≤ x < 100 间的所有数字)
详见:https://leetcode.com/problems/count-numbers-with-unique-digits/description/
C++:
class Solution { public: int countNumbersWithUniqueDigits(int n) { if (n == 0) { return 1; } int res = 0; for (int i = 1; i <= n; ++i) { res += count(i); } return res; } int count(int k) { if (k < 1) { return 0; } if (k == 1) { return 10; } int res = 1; for (int i = 9; i >= (11 - k); --i) { res *= i; } return res * 9; } };
参考:https://www.cnblogs.com/grandyang/p/5582633.html
以上是关于357 Count Numbers with Unique Digits 计算各个位数不同的数字个数的主要内容,如果未能解决你的问题,请参考以下文章
357. Count Numbers with Unique Digits
leetcode [357]Count Numbers with Unique Digits
[leetcode-357-Count Numbers with Unique Digits]
[LeetCode] 357. Count Numbers with Unique Digits
leetCode 357. Count Numbers with Unique Digits | Dynamic Programming | Medium