当前位置:网站首页>leetcode:357. 统计各位数字都不同的数字个数
leetcode:357. 统计各位数字都不同的数字个数
2022-08-10 22:17:00 【OceanStar的学习笔记】
题目来源
题目描述
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
}
};
题目解析
排列组合
思路
首先考虑两种边界情况:
- 当n == 0时, 0 < = x < 1 0 <= x < 1 0<=x<1,x只有1种选择,即0
- 当n == 1时, 0 < = x < 10 0 <= x < 10 0<=x<10,x有10种选择,即0 ~ 9(结果为 n == 0 和 n == 1 的和)
当n == 2时, 0 < = x < 100 0 <= x < 100 0<=x<100,x的选择可以由两部分组成:
- 只有一位数的x:上面已经算过了
- 有两位数的x。由组合数学可得
- 第一位的选择有9种,即 1 9 1 ~ 9 1 9
- 第二位的选择也有9种,即 0 9 0 ~ 9 0 9中除去第一位的选择
- 所以此时一共 9 * 9 + 10 = 91
当n == 3时, 0 < = x < 1000 0 <= x < 1000 0<=x<1000,x的选择可以由三部分组成:
- 只有一位数的x:
- 有两位数的x:
- 有三位数的x:
- 第一位的选择有9种,即 1 9 1 ~ 9 1 9
- 第二位的选择也有9种,即 0 9 0 ~ 9 0 9中除去第一位的选择
- 第三位的选择有8种,即 0 9 0 ~ 9 0 9中除去第一位和第二位的选择
- 所以一共有 9 * 9 * 8 + 10
实现
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n == 0){
return 1;
}
if(n == 1){
return 10;
}
int ans = 10, curr = 9;
for (int i = 0; i < n - 1; ++i) {
curr *= (9 - i);
ans += curr;
}
return ans;
}
};
从上面可以看出,后面的会依赖前面的结论,所以可以用动态规划
动态规划
思路
- n == 0,数字有{0}1个。
- n == 1,数字有{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}10个。
- n == 2,数字包括两部分之和:
- 一部分为n == 1的所有10个答案
- 另一部分为长度为2的新增数字
- 这部分可以在n = 1的所有9个数字的基础上进行拼接(0不能算)。
- 比如:
- 从n=1的数字列表{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}中随便取出一个除0以外的数字(因为0不能作为起始数字!,也就是说有9种选择),我们取2好了。
- 通过在2的尾巴处拼接一位数字可以得到新的合法数字有:{20, 21,23,24,25,26,27,28,29}
- 可以看到,除了不能在尾巴处拼接一个2(两个连续的2就非法了!),0-9种一共有9个数字可以拿来拼接在尾巴处。新增答案为9个。
- 同理,对于n=1数字列表{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}中的其他任意非0数也可以进行拼接操作,一共可以新增9*9个答案。
- 最终,n=2的合法数字,n=1时的答案 + 长度为2的数字个数(9*9个)= 10 + 81 = 91。
- n == 3时同理,只不过此时可以用拼接的数字减少为了8个,此时答案为10 + 9 * 9 + 9 * 9 * 8 = 739。
- 通过归纳不难得到,假设 dp[i] 即 n = i时的答案,则动态转移方程为:dp[i] = dp[i-1] + (dp[i-1] - dp[i-2])*(10-(i-1))
- 转移的初始条件为:dp[0] = 1,dp[1] = 10
边栏推荐
- CIKM2022 | Sequence Recommendation Based on Bidirectional Transformers Contrastive Learning
- IM 即时通讯开发如何设计图片文件的服务端存储架构
- SDP
- Service - DHCP principle and configuration
- August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core
- 音乐播放器(未完成版本)
- 交换机和生成树知识点
- 高通平台开发系列讲解(应用篇)QCMAP应用框架介绍
- 字节跳动原来这么容易就能进去...
- Self-organization is a two-way journey between managers and members
猜你喜欢
What is Jmeter? What are the principle steps used by Jmeter?
shell programming without interaction
谁是边缘计算服务的采购者?是这六个关键角色
Redis
Glide监听Activity生命周期源码分析
链表相加(二)
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
BM13 determines whether a linked list is a palindrome
Pro-test is effective | A method to deal with missing features of risk control data
geemap的详细安装步骤及环境配置
随机推荐
ThreadLocal comprehensive analysis (1)
geemap的详细安装步骤及环境配置
艺术与科技的狂欢,阿那亚2022砂之盒沉浸艺术季
Shell programming specification and variables
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
JS中使用正则表达式g模式和非g模式的区别
STL-stack
OneNote 教程,如何在 OneNote 中整理笔记本?
RK3399 platform development series explanation (kernel-driven peripherals) 6.35, IAM20680 gyroscope introduction
Fatal error: cstring: No such file or directory
VulnHub之DC靶场下载与DC靶场全系列渗透实战详细过程
企业云存储日常运行维护实践经验分享
Regular expression of shell programming and text processor
边缘与云计算:哪种解决方案更适合您的连接设备?
August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core
Extended Chinese Remainder Theorem
3598. 二叉树遍历(华中科技大学考研机试题)
How does the Weiluntong touch screen display the current value of abnormal data while alarming?
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】