当前位置:网站首页>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
边栏推荐
- 12 Recurrent Neural Network RNN2 of Deep Learning
- LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
- FPGA - Memory Resources of 7 Series FPGA Internal Structure -03- Built-in Error Correction Function
- Why general company will say "go back messages such as" after the end of the interview, rather than just tell the interviewer the result?
- LeetCode Daily Question (1573. Number of Ways to Split a String)
- 68:第六章:开发文章服务:1:内容梳理;article表介绍;创建【article】文章服务;
- Black cat takes you to learn Makefile Part 12: Summary of common Makefile problems
- web项目访问引用jar内部的静态资源
- 2021IDEA创建web工程
- GMT,UTC,CST,DST,RTC,NTP,SNTP,NITZ: 嵌入式的时间
猜你喜欢

Leave a message with a prize | OpenBMB x Tsinghua University NLP: The update of the large model open class is complete!

Shell programming specification and variables

Shell 编程--Sed

camera preview process --- from HAL to OEM

Nodes in the linked list are flipped in groups of k

LabVIEW分配多少线程?

Distribution Network Expansion Planning: Consider Decisions Using Probabilistic Energy Production and Consumption Profiles (Matlab Code Implementation)

配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)

高通平台开发系列讲解(应用篇)QCMAP应用框架介绍

12 Recurrent Neural Network RNN2 of Deep Learning
随机推荐
DC-9靶场下载及渗透实战详细过程(DC靶场系列)
过滤器
DC-8靶场下载及渗透实战详细过程(DC靶场系列)
What would happen if disconnecting during the process of TCP connection?
CIKM2022 | 基于双向Transformers对比学习的序列推荐
MySQL: MySQL Cluster - Principle and Configuration of Master-Slave Replication
String类的常用方法
Distribution Network Expansion Planning: Consider Decisions Using Probabilistic Energy Production and Consumption Profiles (Matlab Code Implementation)
Extended Chinese Remainder Theorem
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
OneNote tutorial, how to organize notebooks in OneNote?
Conditional Statements of Shell Programming (2)
【640. 求解方程】
BM13 determines whether a linked list is a palindrome
August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core
unusual understanding
pytorch tear CNN
This visual tool artifact is more intuitive and easy to use!love so much
"DevOps Night Talk" - Pilot - Introduction to CNCF Open Source DevOps Project DevStream - feat. PMC member Hu Tao
如何成为一名正义黑客?你应该学习什么?