-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution241.java
56 lines (51 loc) · 1.67 KB
/
Solution241.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
package algorithm.leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* @author: mayuan
* @desc:
* @date: 2018/08/21
*/
public class Solution241 {
public static void main(String[] args) {
String str1 = "2*3-4*5";
Solution241 test = new Solution241();
List<Integer> ans = test.diffWaysToCompute(str1);
ans.forEach(System.out::println);
}
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ways = new ArrayList<>();
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
if ('+' == c || '-' == c || '*' == c) {
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i + 1));
for (int l : left) {
for (int r : right) {
switch (c) {
case '+': {
ways.add(l + r);
break;
}
case '-': {
ways.add(l - r);
break;
}
case '*': {
ways.add(l * r);
break;
}
default: {
continue;
}
}
}
}
}
}
if (ways.size() == 0) {
ways.add(Integer.valueOf(input));
}
return ways;
}
}