forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
69 lines (66 loc) · 2.25 KB
/
Solution.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
class Solution {
public boolean judgePoint24(int[] nums) {
return dfs(Arrays.stream(nums).boxed().map(Double::new).collect(Collectors.toList()));
}
private boolean dfs(List<Double> numList) {
if (numList.size() == 0) {
return false;
}
if (numList.size() == 1) {
return Math.abs(Math.abs(numList.get(0) - 24.0)) < 0.000001d;
}
for (int i = 0; i < numList.size(); i++) {
for (int j = i + 1; j < numList.size(); j++) {
boolean valid = dfs(getList(numList, i, j, 0)) || dfs(getList(numList, i, j, 1))
|| dfs(getList(numList, i, j, 2)) || dfs(getList(numList, i, j, 3))
|| dfs(getList(numList, i, j, 4)) || dfs(getList(numList, i, j, 5));
if (valid) {
return true;
}
}
}
return false;
}
private List<Double> getList(List<Double> numList, int i, int j, int operate) {
List<Double> candidate = new ArrayList<>();
for (int k = 0; k < numList.size(); k++) {
if (k == i || k == j) {
continue;
}
candidate.add(numList.get(k));
}
switch (operate) {
// a + b
case 0:
candidate.add(numList.get(i) + numList.get(j));
break;
// a - b
case 1:
candidate.add(numList.get(i) - numList.get(j));
break;
// b - a
case 2:
candidate.add(numList.get(j) - numList.get(i));
break;
// a * b
case 3:
candidate.add(numList.get(i) * numList.get(j));
break;
// a / b
case 4:
if (numList.get(j) == 0) {
return Collections.emptyList();
}
candidate.add(numList.get(i) / numList.get(j));
break;
// b / a
case 5:
if (numList.get(i) == 0) {
return Collections.emptyList();
}
candidate.add(numList.get(j) / numList.get(i));
break;
}
return candidate;
}
}