requirements
A self-divisor is a number that can be divisible by every digit it contains.
For example, 128 is a self-divisor because 128%1 == 0, 128%2 == 0, 128%8 == 0.
Also, self-divisor is not allowed to contain 0.
Given upper and lower boundary numbers, prints a list of all the self-divisor elements within the boundary (including the boundary).
Example 1:
Input: upper boundary left = 1, lower boundary right = 22 Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]Copy the code
The core code
class Solution:
def selfDividingNumbers(self, left: int, right: int) - >List[int] :
res = list(a)for i in range(left,right+1):
flag = True
n = i
while n:
num = n % 10
n //= 10
if not num ori % num ! =0:
flag = False
break
if flag:
res.append(i)
return res
Copy the code
We iterate from left to right to see if the number we scan meets the definition of self-divisor. We use a marker bit to mark it. When the condition is met, we add it to our output result.