357 Count Numbers with Unique Digits 计算各个位数不同的数字个数
2024-09-02 14:56:26
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n。
示例:
给定 n = 2,返回 91。(答案应该是除[11,22,33,44,55,66,77,88,99]外,0 ≤ x < 100 间的所有数字)
详见:https://leetcode.com/problems/count-numbers-with-unique-digits/description/
C++:
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n == 0)
{
return 1;
}
int res = 0;
for (int i = 1; i <= n; ++i)
{
res += count(i);
}
return res;
}
int count(int k)
{
if (k < 1)
{
return 0;
}
if (k == 1)
{
return 10;
}
int res = 1;
for (int i = 9; i >= (11 - k); --i)
{
res *= i;
}
return res * 9;
}
};
参考:https://www.cnblogs.com/grandyang/p/5582633.html
最新文章
- Web项目中删错文件怎么办
- 【前台 submit的重复提交 错误】submit的重复提交
- Linux 下没有conio.h 已解决
- 如何设置修改WPS批注上的用户信息名称
- xcode语法高亮插件
- ios9邮箱添加163邮箱
- 设置开机启动时指定非ROOT用户执行相应的脚本
- js 保留两位小数
- sim卡中短信简要格式
- python_基础学习_03_正则替换文本(re.sub)
- git的初步使用---本地代码库的使用和提交
- BZOJ 3211: 花神游历各国【线段树区间开方问题】
- 从壹开始前后端分离 [ vue + .netcore 补充教程 ] 二九║ Nuxt实战:异步实现数据双端渲染
- Docker的介绍及安装
- 2018-2019-2 网络对抗技术 20165206 Exp3 免杀原理与实践
- AppStore关键词覆盖法则标记
- 多线程之BlockingQueue中 take、offer、put、add的一些比较
- Android设置屏幕旋转后保存数据
- Asp.Net Newtonsoft.Json使用教程
- 初探和实现websocket心跳重连(npm: websocket-heartbeat-js)
热门文章
- python文件读写及形式转化和CGI的简单应用
- ndk编译libpcap 1.7.4(最终解决方法)
- MySQL 分库、分表
- 【Codeforces 375A】Divisible by Seven
- 洛谷 P3203 BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
- [luoguP2680] 运输计划(lca + 二分 + 差分)
- 【BZOJ3238】差异(后缀数组,单调栈)
- lombok中的@ToString注解作用
- MYSQL中插入数据以及修改数据的部分方法
- 洛谷 P2683 小岛