当前位置:网站首页>【LeetCode】728. 自除数
【LeetCode】728. 自除数
2022-07-13 18:03:00 【pass night】
题目
自除数 是指可以被它包含的每一位数整除的数。
- 例如,
128是一个 自除数 ,因为128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
自除数 不允许包含 0 。
给定两个整数 left 和 right ,返回一个列表,列表的元素是范围 [left, right] 内所有的 自除数 。
示例 1:
输入:left = 1, right = 22
输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
示例 2:
输入:left = 47, right = 85
输出:[48,55,66,77]
提示:
1 <= left <= right <= 104
思路
- 定义一个函数,若自然数中含有零或是除以自身位数不余零则不是自除数,否则则是
代码
class Solution:
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
res =[]
def isSelfDevidingNumber(n: int):
for i in str(n):
if int(i) ==0 or n % int(i) != 0:
return False
return True
for i in range(left,right+1):
if isSelfDevidingNumber(i): res.append(i)
return res
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
猜你喜欢

小米举办第五届IoT安全峰会,助力行业安全隐私保护

Leetcode lecture - 873 Length of the longest Fibonacci subsequence (difficulty: medium)

Men should be "strong", not "soft", "weak" and "empty"

线程机制与事件机制

七、SAN和NAS环境下的安全实施方案实验报告

Hardware course design: sensor device of multi-function player based on stm32

单向链表实现队列和栈

七、SAN和NAS環境下的安全實施方案實驗報告

JVM目录

When byte hung up, the interviewer asked me DDD, but I didn't know
随机推荐
个人博客学习记录
Search 2D matrix II
SAP OPEN SQL
什么是eventloop(事件循环)?
Five principles of aiops landing (3): Architecture route
Leetcode lecture - 1217 Play chips (difficulty: simple)
unity3d-射线
SAP ABAP BP batch maintenance email address
SAP ABAP BP 批量维护邮箱地址
MySQL error messages errors
SAP BW extraction layer error s:aa 821 (bukrs)
The official case of unity rubyadventure
【LeetCode】1217. 玩筹码
七、SAN和NAS环境下的安全实施方案实验报告
SAP DUMP CX_SY_CONVERSION_NO_NUMBER
Associative word matching - Summary
MySQL operation
English words used by unity
Get started with promise
Volatile最终解释