当前位置:网站首页>leetcode--357. 统计各位数字都不同的数字个数
leetcode--357. 统计各位数字都不同的数字个数
2022-04-23 13:41:00 【爱学习的Amelia】
-
给你一个整数
n
,统计并返回各位数字都不同的数字x
的个数,其中0 <= x < 10n
。 1 -
示例:
# 示例 1
输入:n = 2
输出:91
解释:答案应为除去 11、22、33、44、55、66、77、88、99 外,在 0 ≤ x < 100 范围内的所有数字。
# 示例 2
输入:n = 0
输出:1
-
提示:
0 <= n <= 8
-
思路:
采用递归,先设置递归的出口,若n减到0了,返回1
循环逻辑为:
f(0) = 1
f(1) = 9 + f(0)
f(2) = 9 * 9 + f(1)
f(3) = 9 * 9 * 8 + f(2)
…
设置一个值为9,再在输入的值里面设置一个循环从1遍历到n,分析可知要想定义一个9和另一个9不变,那么加起来的值一定等于10,然后函数的实现如下解法一
返回前一个值的ans加上函数的递归
- 解法一–递归:
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
if n == 0:
return 1
ans = 9
for i in range(1, n):
ans *= (10 - i)
return ans + self.countNumbersWithUniqueDigits(n - 1)
- 思路:
设置了两个函数,首先看cntpo,这个函数是返回每几位数之间的不符合条件的数的总和
countNumbersWithUniqueDigits函数是将每个不符合条件的数的总和加起来,再返回一个总数减去不符合条件的数,即符合条件的数
- 解法二–暴力解法:
class Solution:
def countNumbersWithUniqueDigits(self, n):
s = 0
for i in range(1, n + 1):
j = self.cntpo(i)
s += j
s_sum = pow(10, n)
return s_sum - s
def cntpo(self, n):
cnt = 0
for i in range(pow(10, n-1), pow(10, n)):
for j in str(i):
if str(i).count(j) > 1:
cnt += 1
break
return cnt
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-numbers-with-unique-digits/ ︎
版权声明
本文为[爱学习的Amelia]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46361294/article/details/124357167
边栏推荐
- [machine learning] Note 4. KNN + cross validation
- 为什么从事云原生开发需要学习容器技术
- Static interface method calls are not supported at language level '5'
- Android 面试主题集合整理
- Two ways to deal with conflicting data in MySQL and PG Libraries
- The interviewer dug a hole for me: what's the use of "/ /" in URI?
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
- Oracle calculates the difference between two dates in seconds, minutes, hours and days
- Solution: you have 18 unapplied migration (s) Your project may not work properly until you apply
- Oracle modify default temporary tablespace
猜你喜欢
Leetcode? The first common node of two linked lists
MySQL [read / write lock + table lock + row lock + mvcc]
Opening: identification of double pointer instrument panel
校园外卖系统 - 「农职邦」微信原生云开发小程序
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
Leetcode | 38 appearance array
Dolphin scheduler configuring dataX pit records
Tangent space
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions
Modify the Jupiter notebook style
随机推荐
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
QT calling external program
为什么从事云原生开发需要学习容器技术
Leetcode? The first common node of two linked lists
Modification of table fields by Oracle
MySQL [acid + isolation level + redo log + undo log]
[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
Reading notes: Secure federated matrix factorization
Why do you need to learn container technology to engage in cloud native development
这个SQL语名是什么意思
sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
Oracle creates tablespaces and modifies user default tablespaces
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
Apache Atlas Compilation and installation records
Influence of openssh version on SSH mutual trust creation in RAC environment
SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
Analysis of unused index columns caused by implicit conversion of timestamp
GDB的使用
Oracle lock table query and unlocking method
19c RAC steps for modifying VIP and scanip - same network segment