Formatted question description: https://leetcode.ca/all/728.html

Level

Easy

Description

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:

left = 1, right = 22

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

  • The boundaries of each input argument are 1 <= left <= right <= 10000.

Solution

For each number in the range [left, right], check whether the number is a self-dividing number.

To check whether a number is a self-dividing number, loop over all its digits and check each digit. If there exists a digit that is 0 or a digit such that the number is not divisible by the digit, then the number is not a self-dividing number. Otherwise, the number is a self-dividing number.

Java

  • import java.util.ArrayList;
    import java.util.List;
    
    /**
    
     A self-dividing number is a number that is divisible by every digit it contains.
    
     For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
    
     Also, a self-dividing number is not allowed to contain the digit zero.
    
     Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
    
     Example 1:
     Input:
     left = 1, right = 22
     Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
     Note:
    
     The boundaries of each input argument are 1 <= left <= right <= 10000.
    
     */
    
    public class Self_Dividing_Numbers {
    
        public static void main (String[] args) {
            Self_Dividing_Numbers out = new Self_Dividing_Numbers();
            Solution s = out.new Solution();
    
            System.out.println(s.selfDividingNumbers(21, 22));
        }
    
        class Solution {
    
            List<Integer> list = new ArrayList<>();
    
            public List<Integer> selfDividingNumbers(int left, int right) {
                if (left > right || right <= 0) {
                    return null;
                }
    
                for (int i = left; i <= right; i++) {
                    isSeflDividing(i);
                }
    
                return list;
            }
    
            public void isSeflDividing(int n) {
    
                List<Integer> digits = new ArrayList<>();
                int current = n;
                while (current > 0) {
                    int d = current % 10;
                    if (d == 0) {
                        return;
                    }
                    digits.add(d);
                    current /= 10;
                }
    
                for (int each: digits) {
                    if (n % each != 0) {
                        return;
                    }
                }
    
                list.add(n);
            }
        }
    }
    
  • // OJ: https://leetcode.com/problems/self-dividing-numbers/
    // Time: O(ND) where N = right - left + 1, D = digit count of right.
    // Space: O(1)
    class Solution {
    private:
        bool isSelfDividing(int n) {
            int m = n;
            while (m) {
                int d = m % 10;
                if (!d || n % d != 0) return false;
                m /= 10;
            }
            return true;
        }
    public:
        vector<int> selfDividingNumbers(int left, int right) {
            vector<int> ans;
            for (int i = left; i <= right; ++i) {
                if (isSelfDividing(i)) ans.push_back(i);
            }
            return ans;
        }
    };
    
  • class Solution:
        def isDividingNumber(self, num):
            if '0' in str(num):
                return False
            return 0 == sum(num % int(i) for i in str(num))
        def selfDividingNumbers(self, left, right):
            """
            :type left: int
            :type right: int
            :rtype: List[int]
            """
            answer = []
            for num in range(left, right+1):
                print(num)
                if self.isDividingNumber(num):
                    answer.append(num)
            return answer
    

Java

  • class Solution {
        public List<Integer> selfDividingNumbers(int left, int right) {
            List<Integer> list = new ArrayList<Integer>();
            for (int i = left; i <= right; i++) {
                if (isSelfDividing(i))
                    list.add(i);
            }
            return list;
        }
    
        public boolean isSelfDividing(int num) {
            char[] digitsArray = String.valueOf(num).toCharArray();
            for (char c : digitsArray) {
                int digit = c - '0';
                if (digit == 0 || num % digit != 0)
                    return false;
            }
            return true;
        }
    }
    
  • // OJ: https://leetcode.com/problems/self-dividing-numbers/
    // Time: O(ND) where N = right - left + 1, D = digit count of right.
    // Space: O(1)
    class Solution {
    private:
        bool isSelfDividing(int n) {
            int m = n;
            while (m) {
                int d = m % 10;
                if (!d || n % d != 0) return false;
                m /= 10;
            }
            return true;
        }
    public:
        vector<int> selfDividingNumbers(int left, int right) {
            vector<int> ans;
            for (int i = left; i <= right; ++i) {
                if (isSelfDividing(i)) ans.push_back(i);
            }
            return ans;
        }
    };
    
  • class Solution:
        def isDividingNumber(self, num):
            if '0' in str(num):
                return False
            return 0 == sum(num % int(i) for i in str(num))
        def selfDividingNumbers(self, left, right):
            """
            :type left: int
            :type right: int
            :rtype: List[int]
            """
            answer = []
            for num in range(left, right+1):
                print(num)
                if self.isDividingNumber(num):
                    answer.append(num)
            return answer
    

All Problems

All Solutions