当前位置:网站首页>leetcode--357. Count the number of different figures
leetcode--357. Count the number of different figures
2022-04-23 14:03:00 【Amelia who loves learning】
-
Give you an integer
n
, Count and return numbers with different numbersx
The number of , among0 <= x < 10n
. 1 -
Example :
# Example 1
Input :n = 2
Output :91
explain : The answer should be to remove 11、22、33、44、55、66、77、88、99 Outside , stay 0 ≤ x < 100 All numbers in the range .
# Example 2
Input :n = 0
Output :1
-
Tips :
0 <= n <= 8
-
Ideas :
Recursion , First set the exit of recursion , if n Reduced to 0 了 , return 1
The loop logic is :
f(0) = 1
f(1) = 9 + f(0)
f(2) = 9 * 9 + f(1)
f(3) = 9 * 9 * 8 + f(2)
…
Set a value to 9, Then set a loop in the input value from 1 Traversing n, Analysis shows that to define a 9 And another one. 9 unchanged , Then the sum must be equal to 10, Then the realization of the function is as follows: solution 1
Returns the of the previous value ans Plus the recursion of the function
- Solution 1 – recursive :
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)
- Ideas :
Two functions are set , First of all to see cntpo, This function returns the sum of unqualified numbers between every few digits
countNumbersWithUniqueDigits The function is to add up the sum of each unqualified number , Then return a total minus the unqualified number , That is, the number of qualified
- Solution 2 – Violence solution :
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
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/count-numbers-with-unique-digits/ ︎
版权声明
本文为[Amelia who loves learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231341317327.html
边栏推荐
- 微信小程序的订阅号开发(消息推送)
- Introduction to spark basic operation
- Port occupied 1
- [VMware] address of VMware Tools
- 全局变量能否放在头文件中定义
- 理解虚基类、虚函数与纯虚函数的概念(转)
- What is the difference between blue-green publishing, rolling publishing and gray publishing?
- 金蝶云星空API调用实践
- JS force deduction brush question 103 Zigzag sequence traversal of binary tree
- 商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
猜你喜欢
JMeter pressure test tool
基础知识学习记录
[VMware] address of VMware Tools
微信小程序获取登录用户信息、openid和access_token
商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
Record a strange bug: component copy after cache component jump
Chapter I review of e-commerce spike products
Basic knowledge learning record
基于ibeacons三点定位(微信小程序)
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
随机推荐
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
mysql通过binlog文件恢复数据
freeCodeCamp----time_ Calculator exercise
蓝绿发布、滚动发布、灰度发布,有什么区别?
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
AtCoder Beginner Contest 248C Dice Sum (生成函数)
Force deduction brush question 101 Symmetric binary tree
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
快捷键(多行)
YARN线上动态资源调优
JS 烧脑面试题大赏
全局变量能否放在头文件中定义
Android篇:2019初中级Android开发社招面试解答(中
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
MySQL 修改主数据库
Express middleware ③ (custom Middleware)
_模_板_
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
微信小程序setInterval定时函数使用详细教程