Count Numbers with Unique Digits
2016-06-16 16:42
441 查看
题目描述:
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
思路是:恰好n位数形成无重复数是9*9*8*7....所以求出n位数构成的数,加上countNumbersWithUniqueDigits(n-1)即可。
public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if(n==0)
return 1;
if(n==1)
return 10;
int num=9;
for(int i=0;i<n-1;i++){
num*=(9-i);
}
return num+=countNumbersWithUniqueDigits(n-1);
}
}
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位数形成无重复数是9*9*8*7....所以求出n位数构成的数,加上countNumbersWithUniqueDigits(n-1)即可。
public class Solution {
public int countNumbersWithUniqueDigits(int n) {
if(n==0)
return 1;
if(n==1)
return 10;
int num=9;
for(int i=0;i<n-1;i++){
num*=(9-i);
}
return num+=countNumbersWithUniqueDigits(n-1);
}
}
相关文章推荐
- UITextField使用注意事项——最左侧/右侧添加图片
- Service通过Broadcast更新UI
- BurpSuite+sqlmap: SQLiPy扩展使用说明
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 21.关于使用UICollectionView的一些小技巧
- 百度ueditor组件上传图片后如何设置img标签里的src、title、alt等属性
- easyui js 表单验证
- ListView设置OnTouchListener返回true会怎样
- iOS开发UI篇-懒加载、重写setter方法赋值
- Parquet 之mapreduce
- Java 中break和continue结合标签标示符中断循环示例详解(附源码)
- 有关中标麒麟系统出现sudo 有效用户id不是0,sudo属于root并设置了setuid位吗的问题处理办法
- iOS单选——UITableView 写的列表单选
- CoreException: Could not get the value for parameter compilerId for plugin execution default-compile
- SQLiteDatabase中query、insert、update、delete方法参数说明
- doduicms 手机和pc同步 链接处理
- UITextField输入内容超出输入框光标不移动问题
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
- android studio Gradle Build速度加快方法
- 子线程通过接口更新UI线程