当前位置:网站首页>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
边栏推荐
- Oracle modify default temporary tablespace
- 零拷贝技术
- Leetcode brush question 897 incremental sequential search tree
- Apache Atlas Compilation and installation records
- [code analysis (5)] communication efficient learning of deep networks from decentralized data
- Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
- ACFs file system creation, expansion, reduction and other configuration steps
- Software test system integration project management engineer full truth simulation question (including answer and analysis)
- Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
- Influence of openssh version on SSH mutual trust creation in RAC environment
猜你喜欢
Lenovo Saver y9000x 2020
Oracle job scheduled task usage details
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
SQL learning | set operation
SQL learning window function
MySQL [acid + isolation level + redo log + undo log]
Lenovo Savior y9000x 2020
Zero copy technology
MySQL [SQL performance analysis + SQL tuning]
OSS cloud storage management practice (polite experience)
随机推荐
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
Tensorflow Download
Operations related to Oracle partition
GDB的使用
[code analysis (3)] communication efficient learning of deep networks from decentralized data
Antd design form verification
ACFs file system creation, expansion, reduction and other configuration steps
Test the time required for Oracle library to create an index with 7 million data in a common way
[code analysis (4)] communication efficient learning of deep networks from decentralized data
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
Test on the time required for Oracle to delete data with delete
Interval query through rownum
Apache seatunnel 2.1.0 deployment and stepping on the pit
The interviewer dug a hole for me: what's the use of "/ /" in URI?
MySQL and PgSQL time related operations
聯想拯救者Y9000X 2020
Oracle kills the executing SQL
Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
Lenovo Saver y9000x 2020