357. Count Numbers with Unique Digits
来源:互联网 发布:饶毅毕业致辞知乎 编辑:程序博客网 时间:2024/06/05 23:07
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
思路:一个排列组合的题目,让求没有重复数字的数的个数。当n=1时因为只有一个数字,所以0-9都是答案。当n>=2时,最高位可以为1-9任意一个数字,之后各位可以选择的数字个数依次为9, 8, 7, 6…,上一位选一个下一位就少了一种选择。
class Solution { public int countNumbersWithUniqueDigits(int n) { if(n==0) return 1; if(n==1) return 10; int val = 9, ans = 10; for(int i = 2; i <= n; i++) { val *= (9-i+2); ans += val; } return ans; }}
阅读全文
0 0
- 【LeetCode】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
- 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
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- 357. Count Numbers with Unique Digits
- LeetCode:357. Count Numbers with Unique Digits
- 基础练习 十进制转十六进制
- springboot @Valid标签
- Ubuntu下录制gif动态图
- 如何进行字符串的拷贝?
- 一个菜鸟学习Java 坚持有一个月了 请大佬们多多给晚辈一些建议
- 357. Count Numbers with Unique Digits
- (算法分析Week13)Is Subsequence[Medium]
- Java中的修饰符有关知识整理
- 15、无向图的各连通分支
- Genymotion破解
- AIOps时代,你准备好了吗?
- jvm调优,tomcat溢出查找问题实战
- LeetCode#48. Rotate Image
- 数据库设计总结(原则篇)