-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path2094.Finding-3-Digit-Even-Numbers.java
85 lines (66 loc) · 2.02 KB
/
2094.Finding-3-Digit-Even-Numbers.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// https://leetcode.com/problems/finding-3-digit-even-numbers/
// algorithms
// Easy (48.74%)
// Total Accepted: 5.8K
// Total Submissions: 11.9K
class Solution {
private Set<Integer> set;
public int[] findEvenNumbers(int[] digits) {
List<Integer> digitList = trim(digits);
if (digitList.isEmpty()) {
return new int[]{};
}
Collections.sort(digitList);
set = new HashSet<>();
recursive(digitList, new boolean[digitList.size()], new StringBuilder());
int[] res = new int[set.size()];
int idx = 0;
for (int n : set) {
res[idx++] = n;
}
Arrays.sort(res);
return res;
}
private void recursive(List<Integer> digitList, boolean[] visited, StringBuilder sb) {
int len = digitList.size();
if (sb.length() == 3) {
set.add(Integer.parseInt(sb.toString()));
return;
}
for (int i = 0; i < len; i++) {
if (sb.length() == 0 && digitList.get(i) == 0) {
continue;
}
if (sb.length() == 2 && (digitList.get(i) & 1) == 1) {
continue;
}
if (visited[i]) {
continue;
}
visited[i] = true;
sb.append(digitList.get(i));
recursive(digitList, visited, sb);
sb.deleteCharAt(sb.length() - 1);
visited[i] = false;
}
}
private List<Integer> trim(int[] digits) {
Map<Integer, Integer> map = new HashMap<>();
boolean hasEven = false;
List<Integer> list = new ArrayList<>();
for (int n : digits) {
if ((n & 1) == 0) {
hasEven = true;
}
int curNum = map.getOrDefault(n, 0);
if (curNum < 3) {
list.add(n);
map.put(n, curNum + 1);
}
}
if (!hasEven) {
return Collections.emptyList();
}
return list;
}
}