-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_241.java
More file actions
58 lines (51 loc) · 1.92 KB
/
Copy pathP_241.java
File metadata and controls
58 lines (51 loc) · 1.92 KB
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
package leetcode.medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public final class P_241 {
private static final Pattern KEEP_DELIMITER = Pattern.compile("(?<=[+\\-*])|(?=[+\\-*])");
private static final Pattern DIGITS = Pattern.compile("^[0-9]+$");
public static List<Integer> diffWaysToCompute(String input) {
return recurse(input, new HashMap<>());
}
private static List<Integer> recurse(String input, Map<String, List<Integer>> dp) {
if (DIGITS.matcher(input).matches()) {
return new ArrayList<>(Collections.singletonList(Integer.valueOf(input)));
}
if (dp.containsKey(input)) {
return dp.get(input);
}
final List<Integer> res = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);
if (c == '-' || c == '+' || c == '*') {
final String a = input.substring(0, i);
final String b = input.substring(i + 1);
final List<Integer> al = recurse(a, dp);
final List<Integer> bl = recurse(b, dp);
for (int x : al) {
for (int y : bl) {
if (c == '-') {
res.add(x - y);
} else if (c == '+') {
res.add(x + y);
} else {
res.add(x * y);
}
}
}
}
}
dp.put(input, res);
return res;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(KEEP_DELIMITER.split("12+345-213123*111")));
System.out.println(diffWaysToCompute("2-1-1"));
}
private P_241() {}
}