Count Numbers with Unique Digits -- LeetCode
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
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]
)
思路:DP
dp[i]表示10^i以内的结果数。则10^0 = 1,则dp[0] = 0(只有0一个数),dp[1]=dp[0] + 所有非零的个位数=10.
则dp[i] = dp[i-1] + i位数中的结果数。
求i位数中的结果数很简单。假设我们要求所有3位数中满足要求的数,则最高位只可能是1到9这9个数,因为每一位不能重复,则次高位只能用0到9中没被用过的数共9个,第三位只能用剩下没被用过的0到9中的8个数,因此总数是9 * 9 * 8。 对于i来说,是9 * 9 *...*(11 - i).
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n == ) return ;
int res = ;
for (int i = ; i <= std::min(, n); i++) {
int temp = ;
for (int j = ; j >= - i; j--)
temp *= j;
res += temp;
}
return res;
}
};
Count Numbers with Unique Digits -- LeetCode的更多相关文章
- LC 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode: Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- Java [Leetcode 357]Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- [Swift]LeetCode357. 计算各个位数不同的数字个数 | Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
随机推荐
- SpringMVC 集成 Freemarker 模板引擎
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...
- appium-在页面点击一下处理(一般处理提示蒙层)
在写用例的时候,经常会发现某些第一次进去的页面会有一个蒙层提示.我们只有处理掉这个蒙层,才能继续我们的用例: 这边我们使用的是TouchAction的tap方法 TouchAction action ...
- mac虚拟机上(centos系统)设置联网第二种方式
这种方式简单,不容易出错,用的桥接的方式. 这样的安装的centos会得到ip地址 然后编辑一下网卡配置,使其变为静态得ip 输入命令 # vi /etc/sysconfig/network-scri ...
- Hastable和Dictionary以及ArrayList和(List,LinkedList,数组)的区别
Hastable和Dictionary的区别:(键值对) 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分. 2:多线程程序中推荐使用 Hashtabl ...
- a链接点击下载图片到本地(php)
$url="http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://gandao.my".U('Quest ...
- 网络--NAT技术
一.概述 1.简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task For ...
- android自定义SlideMenu
完美解决ListView中子项焦点不可被Touch的BUG. 1.在Ecl ...
- HDU 1937 F - Finding Seats 枚举
F - Finding Seats Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- PAT 1070 结绳
https://pintia.cn/problem-sets/994805260223102976/problems/994805264706813952 给定一段一段的绳子,你需要把它们串成一条绳. ...
- HttpWebRequest调用WebService后台需要Session信息问题的解决办法
今天在用HttpWebRequest调用后台ASP.NET 的WebService方法时遇到了一个问题,后台的WebService方法里使用到了Session对象中的用户信息,而Session对象中的 ...