-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelfDividingNumbers.java
69 lines (62 loc) · 1.67 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package me.rainking;
import java.util.ArrayList;
import java.util.List;
/**
* @author Rain
* @description
* @date 2018/5/16
*/
public class SelfDividingNumbers {
public static void main(String[] args) {
SelfDividingNumbers o = new SelfDividingNumbers();
System.out.println(o.selfDividingNumbers(1, 22));
}
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> resultList = new ArrayList<>(16);
for (int pos = left; pos < right + 1; pos++) {
if (isSelfDividingNumber(pos)) {
resultList.add(pos);
}
}
return resultList;
}
/**
* 典型的一顿操作猛如虎,一看战绩零杠五
* 效率并不理想
*/
boolean isSelfDividingNumber(int num) {
int[] intArr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int t = num;
while (t != 0) {
int endNum = t % 10;
if (endNum == 0) {
return false;
}
intArr[endNum] = 1;
t /= 10;
}
for (int i = 1; i < 10; i++) {
if (intArr[i] == 1 && num % i != 0) {
return false;
}
}
return true;
}
/**
* 根据子除数定义的解决方式
*/
private boolean isSelfDividingNumber2(int num) {
int inputNumber = num;
while (inputNumber >= 10) {
int digit = inputNumber % 10;
if (digit == 0 || num % digit != 0) {
return false;
}
inputNumber /= 10;
}
if (num % inputNumber != 0) {
return false;
}
return true;
}
}