357:统计各位数字都不同的数字个数
This commit is contained in:
parent
e0cb694765
commit
d3bf1deb4a
@ -0,0 +1,63 @@
|
|||||||
|
//给你一个整数 n ,统计并返回各位数字都不同的数字 x 的个数,其中 0 <= x < 10ⁿ 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 2
|
||||||
|
//输出:91
|
||||||
|
//解释:答案应为除去 11、22、33、44、55、66、77、88、99 外,在 0 ≤ x < 100 范围内的所有数字。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 0
|
||||||
|
//输出:1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= n <= 8
|
||||||
|
//
|
||||||
|
// Related Topics 数学 动态规划 回溯 👍 217 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//357:统计各位数字都不同的数字个数
|
||||||
|
public class CountNumbersWithUniqueDigits {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new CountNumbersWithUniqueDigits().new Solution();
|
||||||
|
// TO TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int countNumbersWithUniqueDigits(int n) {
|
||||||
|
if (n == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (n == 1) {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
int sub = 9;
|
||||||
|
int count = 10;
|
||||||
|
int mul = 9;
|
||||||
|
for (int i = 2; i <= n; i++) {
|
||||||
|
count += mul * sub;
|
||||||
|
mul *= sub;
|
||||||
|
sub--;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
给你一个整数 <code>n</code> ,统计并返回各位数字都不同的数字 <code>x</code> 的个数,其中 <code>0 <= x < 10<sup>n</sup></code><sup> </sup>。
|
||||||
|
<div class="original__bRMd">
|
||||||
|
<div>
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 2
|
||||||
|
<strong>输出:</strong>91
|
||||||
|
<strong>解释:</strong>答案应为除去 <code>11、22、33、44、55、66、77、88、99 </code>外,在 0 ≤ x < 100 范围内的所有数字。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 0
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>0 <= n <= 8</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数学</li><li>动态规划</li><li>回溯</li></div></div><br><div><li>👍 217</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user