357. Count Numbers with Unique Digits
来源:互联网 发布:股票交易系统测试软件 编辑:程序博客网 时间:2024/05/22 17:34
357. Count Numbers with Unique Digits
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,求在(0 ≤ x < 10^n)中没有重复数字的x的个数。如n=2时,返回91。
我列举了0-4的推导:
n 0 f(0) 1
1 f(1) (9+f(0))
2 f(2) (9*9+f(2))
3 f(3) (9*9*8+f(3))
4 f(4) (9*9*8*7+f(4))
可以看出f(n) = 9*9*8*……*(9-n+2)+f(n-1)
这样就可以很快做出来了。
class Solution {public:int countNumbersWithUniqueDigits(int n) {if (n == 0) return 1;int j = n - 1;int x = 9;int i = 9;while (j--) {x = x*i;i--;}return x + countNumbersWithUniqueDigits(n - 1);}};
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
- c++作业3
- python3.6 与MYSQL的安装与连接
- WebService入门程序
- 蓝桥杯 剪邮票
- C#实现office文档转换为PDF或xps的一些方法
- 357. Count Numbers with Unique Digits
- 多线程控件讲解
- Servlet学习笔记二
- HTTP 协议入门
- 关于LayoutInflater的总结
- 窗体泄露 android.view.WindowLeaked: Activity XxxActivity has leaked window android.widget.PopupWindow$Po
- Java 数组转List
- 数据存储,实现用户登录界面及记住密码的实现。
- netfilter bug scheduling while atomic: swapper/0/0x10000100