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 ...
随机推荐
- 一个初学者的辛酸路程-jQuery
前言: 主要概要: 1.HTML+CSS补充 2.DOM事件 3.jQuery示例 内容概要: 1.布局 代码如下 <!DOCTYPE html> <html lang=" ...
- CodeForces-1061B Views Matter
题目链接 https://vjudge.net/problem/CodeForces-1061B 题面 Description You came to the exhibition and one e ...
- Leetcode 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
- html span和div的区别
div与span区别及用法 div与span区别及用法 DIV与SPAN区别及div与san用法篇 接下来了解在div+css开发的时候在html网页制作,特别是标签运用中div和span的区别及用法 ...
- C++寒假学习计划
课程 中国大学mooc西北工业大学c++程序设计 理由 本课程有48节,章节分类清晰,由许多小知识块组成,条例清晰便于学习,由基础开始,由浅入深,适合我这种小白. 计划 从2.8号至2.28除去2.1 ...
- 使用ADO.NET 实体数据模型连接MySql
原文:使用ADO.NET 实体数据模型连接MySql 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a123_z/article/details/8 ...
- 实用JS系列——事件类型
事件就是用户对窗口上各种组件的操作.JS中的事件中的事件即由访问Web页面的用户引起的一系列的操作.一般用于浏览器和用户操作进行交互,例如:用户的单击事件等. 类型分为: 内联模型.脚本模型和DOM2 ...
- Linux学习15_CentOS6.5下netcat工具安装教程
1.下载 下载地址:http://sourceforge.net/projects/netcat/files/netcat/0.7.1/ 下载的是netcat-0.7.1.tar.gz版本 2.拷贝 ...
- crond守护进程
Linux系统任务计划/etc/crontab cron的主配置文件,可以定义PATHcron格式如下:# .----------------分钟 (0 - 59)# | .------------- ...
- hdu 2578 Dating with girls(1) (hash)
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...