A self-dividing number is a number that is divisible by every digit it contains.
For example, 128 is a self-dividing number because128 % 1 == 0
,128 % 2 == 0
, and128 % 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.
这个题也没什么特殊方法,就一个一个地检查是不是self-dividing就可以了
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> res = new ArrayList<Integer>(right - left + 1);
for(int i = left; i <= right; i++) {
if(isSelfDivisible(i)) {
res.add(i);
}
}
return res;
}
public boolean isSelfDivisible(int num) {
int n = num;
while(num > 0) {
int digit = num % 10;
if(digit == 0)
return false;
if(n % digit != 0)
return false;
num /= 10;
}
return true;
}
}