Algorithm


Problem Name: 728. Self Dividing Numbers

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.

A self-dividing number is not allowed to contain the digit zero.

Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].

 

Example 1:

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

Example 2:

Input: left = 47, right = 85
Output: [48,55,66,77]

 

Constraints:

  • 1 <= left <= right <= 104

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int>res;
        for(int i = left; i  < = right; i++)
            if(isValid(i)) res.push_back(i);
        return res;
    }
    
    bool isValid(int num){
        int n = num;
        while(n){
            if(!(n % 10) || num % (n % 10)) return false;
            n /= 10;
        }
        return true;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
left = 1, right = 22

Output

x
+
cmd
[1,2,3,4,5,6,7,8,9,11,12,15,22]

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List selfDividingNumbers(int left, int right) {
    List ans = new ArrayList<>();
    for (int i = left; i  < = right; i++) {
      if (isSelfDivisible(i)) {
        ans.add(i);
      }
    }
    return ans;
  }
  
  private boolean isSelfDivisible(int num) {
    int copy = num;
    while (copy > 0) {
      int rem = copy % 10;
      copy /= 10;
      if (rem == 0 || num % rem != 0) {
        return false;
      }
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
left = 1, right = 22

Output

x
+
cmd
[1,2,3,4,5,6,7,8,9,11,12,15,22]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def selfDividingNumbers(self, left, right):
        """
        :type left: int
        :type right: int
        :rtype: List[int]
        """
        return [num for num in range(left,right+1) if len([char for char in str(num) if int(char)!=0 and num%int(char)==0])==len(str(num)) ]
Copy The Code & Try With Live Editor

Input

x
+
cmd
left = 47, right = 85

Output

x
+
cmd
[48,55,66,77]

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0728_SelfDividingNumbers
    {
        public IList < int> SelfDividingNumbers(int left, int right)
        {
            var result = new List<int>();
            for (int i = left; i  < = right; i++)
                if (IsSelfDividing(i))
                    result.Add(i);

            return result;
        }

        private bool IsSelfDividing(int num)
        {
            if (num > 0 && num  <  9) return true;

            var number = num;
            while (num > 0)
            {
                var current = num % 10;
                if ((current == 0) || (number % current != 0))
                    return false;

                num /= 10;
            }
            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
left = 47, right = 85

Output

x
+
cmd
[48,55,66,77]
Advertisements

Demonstration


Previous
#726 Leetcode Number of Atoms Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#729 Leetcode My Calendar I Solution in C, C++, Java, JavaScript, Python, C# Leetcode