当前位置:网站首页>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
边栏推荐
- SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
- Oracle creates tablespaces and modifies user default tablespaces
- OSS cloud storage management practice (polite experience)
- Generate 32-bit UUID in Oracle
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
- [code analysis (4)] communication efficient learning of deep networks from decentralized data
- Express中间件③(自定义中间件)
- Oracle lock table query and unlocking method
- PG library to view the distribution keys of a table in a certain mode
- Parameter comparison of several e-book readers
猜你喜欢

SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置

TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
![[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions](/img/db/2e7d7f519f97f0c4179a29af8942a6.png)
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions

Common types and basic usage of input plug-in of logstash data processing service

Solution of discarding evaluate function in surprise Library

Detailed explanation of constraints of Oracle table

SQL learning | complex query

Lenovo Saver y9000x 2020

10g database cannot be started when using large memory host
![[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing](/img/12/a330b5e77921bbfa8e96fcbc660daa.png)
[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
随机推荐
Aicoco AI frontier promotion (4.23)
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
Apache Atlas Compilation and installation records
Window function row commonly used for fusion and de duplication_ number
About me
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
Resolution: argument 'radius' is required to be an integer
Logstash数据处理服务的输入插件Input常见类型以及基本使用
Oracle creates tablespaces and modifies user default tablespaces
Database transactions
Plato farm, a top-level metauniverse game, has made frequent positive moves recently
On the bug of JS regular test method
UNIX final exam summary -- for direct Department
Innobackupex incremental backup
Express中间件③(自定义中间件)
Operations related to Oracle partition
Reading notes: Secure federated matrix factorization
Storage scheme of video viewing records of users in station B
Ai21 labs | standing on the shoulders of giant frozen language models
Zero copy technology