Count Numbers with Unique Digits
2016-07-26 12:38
513 查看
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
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])
public class Solution { public int countNumbersWithUniqueDigits(int n) { if (n < 1) return 1; if (n == 1) return 10; // 0 - 10 int count = countNumbersWithUniqueDigits(n - 1); // 1 到 n-1 位数。 int result = 9; // there are 9 choices in the first number. 9 in the second number, 8 in the third, 7 in the fourth number....... for (int i = 0; i < n - 1; i++) { // n位数 result = result * (9 - i); } return count + result; } }
相关文章推荐
- RecyclerView控件补充
- Android UI高级之RecyclerView
- [LeetCode] 52. N-Queens II
- UE4设置Visual Studio
- c# Queue 队列的使用
- 将UINavgationController的push改成从左到右
- 自定义UISearchBar的背景颜色、文本框等信息
- [leetcode] 225. Implement Stack using Queues
- 实现文件下载
- [Codeforces 67C] Sequence of Balls (字符串DP)
- EQueue - 一个C#写的开源分布式消息队列的总体介绍
- luigi学习4-构建工作流
- java continue和break的使用
- PHP中include和require的区别详解
- easyui datagrid 没数据时显示滚动条
- iOS-调整UIButton的title和image的位置
- 动态构建easyUI grid
- IOS:UITest单元测试
- Blueprints - 蓝图之间的交互
- 关于js 构造 onclick 方法中传递Guid参数问题