【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 ...
随机推荐
- C++你不知道的事
class A { public: A() { cout<<"A's constructor"<<endl; } virtual ~A() { cout&l ...
- 学习java的第九天
一.今日收获 1.java完全学习手册第二章程序流程控制中的顺序结构与选择结构 2.学习了java中选择的一些语句和关键词 二.今日问题 1.例题验证有错的情况 2.哔哩哔哩教学视频的一些术语不太理解 ...
- 关于C语言中不同类型数据进行计算 有符号和无符号数进行计算
float是8个有效位, 做个试验: 输出如下: 上面说明了什么: 1, 18/2.2 是除不尽的, 因为是define,所以没有给ratio变量赋值类型,但是从sizeof输出的结果是8,所以系统默 ...
- Default Assignment Operator and References
We have discussed assignment operator overloading for dynamically allocated resources here . This is ...
- LoadRunner中怎么设置密码参数化与用户名关联
对密码参数化时从parameter里的"Select next row"列表中选择Same Line As这一选项,意思就是每一个密码参数化取值与对应行的用户名关联起来了
- 阿里云esc 安装 mysql5.7.27
1. 下载: wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...
- Centos 7 安装redis,修改配置文件不生效、外网不能访问。
前提: 在用Centos 7 安装 redis 时,遇上一下几个问题 ,记录下 . 1.修改配置文件,按官网步骤启动,不生效. 2.外网无法访问redis. 步骤: 1.打开centos 虚拟机 ,按 ...
- Linux 性能优化笔记:应用监控
指标监控 跟系统监控一样,在构建应用程序的监控系统之前,首先也需要确定,到底需要监控哪些指标.特别是要清楚,有哪些指标可以用来快速确认应用程序的性能问题. 对系统资源的监控,USE 法简单有效,却不代 ...
- @Deprecated注解功能
@Deprecated注解功能 标记不建议使用的方法,但是仍然可以用 当方法有更好的方法替换时,但是此方法还有使用时可以使用该注解
- Apache设置虚拟机端口
Apache虚拟机设置端口,以45184端口为例httpd-vhosts.conf文件NameVirtualHost *:45184<VirtualHost *:45184> Doc ...