当前位置:网站首页>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
边栏推荐
- Service - DHCP principle and configuration
- QT笔记——QT工具uic,rcc,moc,qmake的使用和介绍
- CIKM2022 | Sequence Recommendation Based on Bidirectional Transformers Contrastive Learning
- BM7 list entry in central
- This visual tool artifact is more intuitive and easy to use!love so much
- 文件IO-缓冲区
- IM 即时通讯开发如何设计图片文件的服务端存储架构
- BM13判断一个链表是否为回文结构
- C # Hex file transfer skills necessary article 】 【 bin file code implementation
- 从斐波那契 - 谈及动态规划 - 优化
猜你喜欢

BM7 链表中环的入口结点

shell (text printing tool awk)

OneNote 教程,如何在 OneNote 中整理笔记本?

JS学习 2022080

【640. 求解方程】

win系统下pytorch深度学习环境安装

ThreadLocal comprehensive analysis (1)

How to be a Righteous Hacker?What should you study?

HighTec shortcut keys (Keys) setting location
![Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]](/img/d0/13ae2b9987a4fff6f28607a0c01b58.gif)
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
随机推荐
Glide监听Activity生命周期源码分析
Merge k sorted linked lists
Spark基础【RDD转换算子】
Introduction to the use of counter instructions in Rockwell AB PLC RSLogix5000
BM13判断一个链表是否为回文结构
阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布
企业云存储日常运行维护实践经验分享
德科立科创板上市:年营收7.3亿 市值59亿
shell programming without interaction
virtual address space
Black cat takes you to learn Makefile Part 11: When the header file a.h changes, how to recompile all the .c files that depend on the header file a.h
IM 即时通讯开发如何设计图片文件的服务端存储架构
OneNote tutorial, how to organize notebooks in OneNote?
美味的佳肴
Leave a message with a prize | OpenBMB x Tsinghua University NLP: The update of the large model open class is complete!
"DevOps Night Talk" - Pilot - Introduction to CNCF Open Source DevOps Project DevStream - feat. PMC member Hu Tao
Shell编程规范与变量
Use Cloudreve to build a private cloud disk
从斐波那契 - 谈及动态规划 - 优化
留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!