forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifferenceWaysToAddParentheses.java
36 lines (28 loc) · 1.04 KB
/
DifferenceWaysToAddParentheses.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
import java.util.ArrayList;
import java.util.List;
public class DifferenceWaysToAddParentheses {
public List<Integer> diffWaysToCompute(String input) {
int len = input.length();
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < len; i++) {
char c = input.charAt(i);
if ("+-*".indexOf(c) >= 0) {
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i + 1));
for (Integer m : left) {
for (Integer n : right) {
switch (c) {
case '+': result.add(m + n); break;
case '-': result.add(m - n); break;
case '*': result.add(m * n); break;
}
}
}
}
}
if (result.isEmpty()) {
result.add(Integer.valueOf(input));
}
return result;
}
}