Readable Python solution, no casting to str


  • 0
    N
    class Solution(object):
        def selfDividingNumbers(self, left, right):
            """
            :type left: int
            :type right: int
            :rtype: List[int]
            """
            result = []
            for i in xrange(left, right + 1):
                dividers = self.get_dividers(i)
                if dividers and all(i % div == 0 for div in dividers):
                    result.append(i)
    
            return result
    
        def get_dividers(self, i):
            number = i
            dividers = []
            while number > 0:
                number, y = divmod(number, 10)
                if y == 0:
                    return []
    
                dividers.append(y)
            return dividers
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.