https://leetcode.com/problems/self-dividing-numbers/description/
The boundaries of each input argument are
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList();
for (int n = left; n <= right; ++n) {
if (selfDividing(n)) ans.add(n);
}
return ans;
}
public boolean selfDividing(int n) {
for (char c: String.valueOf(n).toCharArray()) {
if (c == '0' || (n % (c - '0') > 0))
return false;
}
return true;
}
/*
Alternate implementation of selfDividing:
public boolean selfDividing(int n) {
int x = n;
while (x > 0) {
int d = x % 10;
x /= 10;
if (d == 0 || (n % d) > 0) return false;
}
return true;
*/
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:
1 <= left <= right <= 10000
.List<Integer> ans = new ArrayList();
for (int n = left; n <= right; ++n) {
if (selfDividing(n)) ans.add(n);
}
return ans;
}
public boolean selfDividing(int n) {
for (char c: String.valueOf(n).toCharArray()) {
if (c == '0' || (n % (c - '0') > 0))
return false;
}
return true;
}
/*
Alternate implementation of selfDividing:
public boolean selfDividing(int n) {
int x = n;
while (x > 0) {
int d = x % 10;
x /= 10;
if (d == 0 || (n % d) > 0) return false;
}
return true;
*/
public List<Integer> selfDividingNumbers(int left, int right) {
return IntStream.rangeClosed(left, right).boxed().filter(this::isSelfDividingNumber).collect(Collectors.toList());
}
private boolean isSelfDividingNumber(int num) {
return !String.valueOf(num).chars().map(li -> li - '0').boxed().anyMatch(i -> i == 0 || num % i != 0);
// Set<Integer> set = String.valueOf(num).chars().map(li -> li -
// '0').boxed().collect(Collectors.toSet());
//
// for (int div : set) {
// if (div == 0)
// return false;
// if (num % div != 0)
// return false;
// }
//
// return true;
}
https://leetcode.com/problems/self-dividing-numbers/discuss/109424/Java-solution-with-explanation-no-toString()-conversion
The idea is to traverse each integer sequence from left to right incrementing by one (left, left + 1, left + 2,..., left + n, right). Each time check if the the current number i is self-divided.
Self-division check is done by using '%' operator(we check each digit of i moving from right to left)
E.g. if i = 128 number:
Self-division check is done by using '%' operator(we check each digit of i moving from right to left)
E.g. if i = 128 number:
- 128 % 10 = 8, check 8 != 0;
- remove 8 from next step j = 128 / 10 = 12
- repeat 1 and 2 until j == 0
Time complexity is O(nm), where n = right - left, and m is number of digits in iterated number
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++) {
int j = i;
for (; j > 0; j /= 10) {
if ((j % 10 == 0) || (i % (j % 10) != 0)) break;
}
if (j == 0) list.add(i);
}
return list;
}
https://leetcode.com/problems/self-dividing-numbers/discuss/109458/JavaC++-Clean-Code