【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/count-numbers-with-unique-digits/description/
题目描述
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
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]
)
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
题目大意
给出了一个n,找出n位的10进制数中,有多少个数字是不包含重复数字的。
解题方法
这个题明显不能用暴力解法,想都不用想。
还是找规律吧:
- 如果n = 1,那么可以有10个数字不同(0~9)
- 如果n >= 2,那么第一位可以是1~9共9个数字,第二位可以是出去第一位的数字+0共9个数字,之后的每位数字都必须不能使用前面已经用过的数字所以依次递减。即9,9,8,7,…,1
- n位数字中由不同的数字构成的数字,是比它小的各位数字所能构成的该条件的数字求和。
使用循环求解,根据数字的位数,来求这个位数的能够满足条件的个数。ans是小于等于n位的求和。
如果看不明白代码,可以这么理解:题目要求的是0 ≤ x < 10^n的x个数,那么x可以为1位数,2位数……n位数。当x为1位数的时候有10个结果;当x为2位数的时候,有99个结果;当x为3位数的时候,有998个结果……也就是说当x为n位数的时候,有99*…*(11 - n个结果),其中n必须小于等于10了(11位数字不可能每一位都不相同)。最后求和就好。
Python代码如下:
class Solution(object):
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
nums = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1]
ans, product = 1, 1
for i in range(min(n, 10)):
product *= nums[i]
ans += product
return ans
C++代码如下:
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int count[]= {9,9,8,7,6,5,4,3,2,1};
int res = 1, prod = 1;
for (int i = 0; i < min(10, n); i ++) {
prod *= count[i];
res += prod;
}
return res;
}
};
日期
2018 年 6 月 2 日 —— 周末在学习
2018 年 12 月 20 日 —— 感冒害的我睡不着
【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)的更多相关文章
- 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
题目描述: 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 ...
随机推荐
- Excel-计算年龄、工龄 datedif()
函数名称:DATEDIF 主要功能:计算返回两个日期参数的差值. 使用格式:=DATEDIF(date1,date2,"y").=DATEDIF(date1,date2," ...
- Oracle-一张表中增加计算某列值重复的次数列,并且把表中其他列也显示出来,或者在显示过程中做一些过滤
总结: 1.计算某列值(数值or字符串)重复的次数 select 列1,count( 列1 or *) count1 from table1 group by 列1 输出的表为:第一列是保留唯一值的 ...
- [云原生]Docker - 简介
目录 什么是Docker? 为什么使用Docker? 对比传统虚拟机总结 什么是Docker? Docker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业务项目.它基于Go ...
- 学习java 7.14
学习内容: 标准输入输出流 输出语言的本质:是一个标准的输出流 字节打印流 字符打印流 对象序列化流 明天内容: 进程和线程 遇到问题: 用对象序列化流序列化一个对象后,假如我们修改了对象所属的类文件 ...
- Identity Server 4 从入门到落地(七)—— 控制台客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Yarn【架构、原理、多队列配置】
目录 一.什么是yarn 二.yarn的基本架构和角色 三.yarn的工作机制 四.任务提交流程 五.资源调度器 FIFO 容量调度器 公平调度器 六.容量调度器多队列提交案例实操 1.案例:配置de ...
- Vue面试专题(未完)
1.谈一下你对MVVM原理的理解 传统的 MVC 指的是,用户操作会请求服务端路由,路由会调用对应的控制器来处理,控制器会获取数 据.将结果返回给前端,页面重新渲染. MVVM :传统的前端会将数 ...
- OC-copy,单例
总结 编号 主题 内容 一 NSFileManager NSFileManager介绍/用法(常见的判断)/文件访问/文件操作 二 集合对象的内存管理 集合对象的内存管理/内存管理总结 三 *copy ...
- html框架frame iframe
框架 通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面.没分HTML文档称作一个框架. 缺点: 开发人员必须同时跟踪更多的HTML文档 很难打印整张页面 框架结构标签(<frameset ...
- arcgis api for js自定义引用方式
(1)常规模式 即arcgis js常见的模块引用方式,采用 require-function 模式,function的参数与require一一对应即可(dojo/domReady!比较特殊,无需 ...