-
Notifications
You must be signed in to change notification settings - Fork 0
/
241DifferentWaystoAddParentheses.java
34 lines (34 loc) · 1.36 KB
/
241DifferentWaystoAddParentheses.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
//将大问题拆为小问题逐个解决
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i < input.length(); i++) {
if(input.charAt(i) == '+' ||
input.charAt(i) == '-' ||
input.charAt(i) == '*') {
String part1 = input.substring(0, i);
String part2 = input.substring(i+1);
List<Integer> res1 = diffWaysToCompute(part1);
List<Integer> res2 = diffWaysToCompute(part2);
int sum = 0;
for(int num1: res1) {
for(int num2: res2) {
switch(input.charAt(i)) {
case '+':
sum = num1 + num2;
break;
case '-':
sum = num1 - num2;
break;
case '*':
sum = num1 * num2;
}
res.add(sum);
}
}
}
}
if(res.size() == 0) res.add(Integer.valueOf(input));
return res;
}
}