-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelfDividingNumbers.java
42 lines (36 loc) · 1.07 KB
/
SelfDividingNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/self-dividing-numbers/
public class SelfDividingNumbers {
private final int left;
private final int right;
public SelfDividingNumbers(int left, int right) {
this.left = left;
this.right = right;
}
public List<Integer> solution() {
List<Integer> result = new ArrayList<>();
for (int i = left; i <= right; i++) {
int num = i;
boolean isSelfDividing = true;
while (num != 0) {
int candidate = num % 10;
if (candidate == 0) {
isSelfDividing = false;
break;
}
int rem = i % candidate;
if (rem != 0) {
isSelfDividing = false;
break;
}
num /= 10;
}
if (isSelfDividing) {
result.add(i);
}
}
return result;
}
}