[leetcode-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]
)
思路:
思路:
排列组合题。
设i为长度为i的各个位置上数字互不相同的数。
- i==1 : 1 0(0~9共10个数,均不重复)
- i==2: 9 * 9 (第一个位置上除0外有9种选择,第2个位置上除第一个已经选择的数,还包括数字0,也有9种选择)
- i ==3: 9* 9 * 8 (前面两个位置同i==2,第三个位置除前两个位置已经选择的数还有8个数可以用)
- ……
- i== n: 9 * 9 * 8 *…… (9-i+2)
需要注意的是,9- i + 2 >0 即 i < 11,也就是i最大为10,正好把每个数都用了一遍。
参考自:https://www.hrwhisper.me/leetcode-count-numbers-unique-digits/
int countNumbersWithUniqueDigits(int n)
{
if (n == )return ;
if (n == )return ;
vector<int>dp(n+,);
n = min(n,);
int cnt = ;
for (int i = ; i <= n;i++)
{
int j = - i;
dp[i] = dp[i - ] * j;
cnt += dp[i];
}
return cnt;
}
[leetcode-357-Count Numbers with Unique Digits]的更多相关文章
- 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. ...
- 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】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- 357. 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 计算各个位数不同的数字个数
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...
- [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: 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 -- LeetCode
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n. Exam ...
- [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 ...
随机推荐
- Vmware虚拟机中安装cnetOS7详细图解步骤
1.安装VMware 下载一个软件安装: .新建一个虚拟机 .引用安装包 4.启动新建的虚拟机 .安装CentOS7的步骤 配置系统语言: 配置系统时间: 配置系统键盘: 配置键盘切换的快捷键: 配置 ...
- MFC教程
MFC教程 还有VS2015的视频教程 试看教程地址:http://dwz.cn/4PcfPk免费下载地址:http://dwz.cn/mfc888 一.VS2010/MFC编程入门教程之目录 第一部 ...
- RabbitMQ学习2---使用场景
RabbitMQ主页:https://www.rabbitmq.com/ AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Serve ...
- JDK的并发容器
除了提供诸如同步控制,线程池等基本工具外,为了提高开发人员的效率,JDK已经为我们准备了一大批好用的并发容器,这些容器都是线程安全的,可以大大减少开发工作量.你可以在里面找到链表.Hash ...
- 修改别人写的利用AOP实现日志监控的问题
原文链接 http://blog.csdn.net/jaune161/article/details/51476138 想法 看到原文博主写的这篇文章,我感觉写的很好,可以在我们的项目中使用AOP来监 ...
- 手机端的viewport属性
Window.devicePixelRatioThis read-only property returns the ratio of the resolution in physical pixel ...
- 【.net 深呼吸】通过标准输入/输出流来完成进程间通信
实现进程之间煲电话粥的方式,有好几种,比如,你可以用这些方案: 1.使用socket来传递.这个好像很无聊,本地进程之间也用socket?不过,通过本机回环网络确实可以进程之间通信. 2.WCF,与上 ...
- Updates were rejected because the remote contains work that you do
每次建立新的仓库,提交的时总会出现这样的错误,真是头疼,...... 直接开始正题,git 提交的步骤: 1. git init //初始化仓库 2. git add .(文件name) //添加文件 ...
- android相对布局中控件的常用属性【转】
Android布局属性详解 RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHorizontal 水平居中 andr ...
- git上传本地项目到github
git软件下载地址:https://git-scm.com/download/ 1. 在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击 ...