↓Skip to main content
  1. LeetCode/

LeetCode 728: Self Dividing Numbers

·2 mins· ·
LeetCode Easy Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-31

  • Submissions: Wrong Answer ×2 → Runtime Error ×3 → Wrong Answer → Runtime Error ×7 → Accepted ×2 → Runtime Error ×2 → Accepted

Approach
#

The range is small (at most 10^4), so check each number. Peel digits with % 10 and // 10 (Math Template 5): any 0 digit disqualifies it (no division by zero, and the problem forbids zeros), otherwise require x % d == 0.

My Attempts
#

This took 18 submissions:

  • Attempt 1 forgot the return; attempt 2 tested divisibility with //, and i // d != 0 is almost always true, so every two-digit number was excluded: (2, 11) lost 11.
  • After switching to %, numbers with a 0 such as 10 raised ZeroDivisionError for a dozen more tries. One version skipped the 0 digit instead, which let 10 in, though a number containing 0 is never self-dividing.
  • A helper that returns False on a 0 digit finally passed.

Solution
#

class Solution:
    def selfDividingNumbers(self, left: int, right: int) -> List[int]:
        def ok(x):
            n = x
            while n:
                d = n % 10
                if d == 0 or x % d:
                    return False
                n //= 10
            return True
        return [x for x in range(left, right + 1) if ok(x)]

Complexity
#

Time O((right - left) · log right), space O(1) excluding output.

Mistakes to Avoid
#

  • Divisibility needs %, not //.
  • A number containing 0 is disqualified, not just that digit skipped.

Interview Explanation
#

I check each number in the range, peeling digits with modulo; a zero digit or a non-divisor disqualifies it. Time is the range length times the digit count.

Related

LeetCode 628: Maximum Product of Three Numbers
·1 min
LeetCode Easy Array Math Sorting
LeetCode 628 note: after sorting there are only two candidates: the three largest, or the two smallest (negatives) times the largest
LeetCode 598: Range Addition II
·2 mins
LeetCode Easy Array Math
LeetCode 598 note: every operation covers the top-left corner, so the most-incremented cells form the intersection of all operations: min(a) × min(b)
LeetCode 504: Base 7
·1 min
LeetCode Easy Math String
LeetCode 504 note: repeatedly take % 7 and // 7 of the absolute value, then add the sign and reverse; handle 0 separately
LeetCode 509: Fibonacci Number
·1 min
LeetCode Easy Math Dynamic-Programming Recursion Memoization
LeetCode 509 note: the starting point of 1D DP; only the previous two terms are needed, so roll two variables
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 441: Arranging Coins
·1 min
LeetCode Easy Math Binary-Search
LeetCode 441 note: find the largest k with k(k + 1) / 2 <= n; when moving right write l = mid + 1 or the search gets stuck