Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 1.06 KB

File metadata and controls

37 lines (29 loc) · 1.06 KB

728. 自除数

自除数 是指可以被它包含的每一位数除尽的数。

例如,128 是一个自除数,因为 128 % 1 == 0128 % 2 == 0128 % 8 == 0

还有,自除数不允许包含 0 。

给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。

示例 1:

Input:
上边界left = 1, 下边界right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

注意:

  • 每个输入参数的边界满足 1 <= left <= right <= 10000

题解 (Python)

1. 题解

class Solution:
    def selfDividingNumbers(self, left: int, right: int) -> List[int]:
        return list(filter(self.isSelfDividingNumber, range(left, right + 1)))
    
    def isSelfDividingNumber(self, num: int) -> bool:
        if num == 0:
            return False
        n = num
        while num != 0:
            if num % 10 == 0 or n % (num % 10) != 0:
                return False
            num //= 10
        return True