- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionAddOperation.java
More file actions
Latest commit
134 lines (117 loc) · 4.26 KB
/
Copy pathExpressionAddOperation.java
File metadata and controls
134 lines (117 loc) · 4.26 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Stack;
publicclassExpressionAddOperation {
/**
Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
Example 1:
Input: num = "123", target = 6
Output: ["1+2+3", "1*2*3"]
Example 2:
Input: num = "232", target = 8
Output: ["2*3+2", "2+3*2"]
Example 3:
Input: num = "105", target = 5
Output: ["1*0+5","10-5"]
Example 4:
Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]
Example 5:
Input: num = "3456237490", target = 9191
Output: []
*/
publicList<String> addOperators(Stringnum, inttarget) {
List<String> res = newArrayList<>();
if (num == null || num.length() == 0) returnres;
// 1. partition the numbers
List<List<Long>> partitions = newArrayList<>();
partition(partitions, newArrayList<>(), num); // n!
// 2. try different operator
for (List<Long> numsList : partitions) { // m
List<String> formulars = newArrayList<>();
formFormulars(formulars, newStringBuilder(), numsList, 0); // O(3 ^ k)
for (Stringformular : formulars) {
if (result(formular) == (long)target) {
System.out.println(formular);
System.out.println(result(formular));
res.add(formular); // O(n)
}
}
}
returnres;
}
publicvoidpartition(List<List<Long>> res, List<Long> tmp, Stringnum) {
if (num.length() == 0) {
res.add(newArrayList<>(tmp));
return;
}
for(inti = 1; i <= num.length(); i++) {
StringcurNUm = num.substring(0, i);
if (curNUm.charAt(0) == '0' && i > 1) continue;
tmp.add(Long.valueOf(curNUm));
partition(res, tmp, num.substring(i));
tmp.remove(tmp.size() - 1);
}
}
publicvoidformFormulars(List<String> res, StringBuildertmp, List<Long> numList, intstart) {
intoriSize = tmp.length();
tmp.append(numList.get(start));
if (start >= numList.size() - 1) {
res.add(tmp.toString());
tmp.delete(oriSize, tmp.length());
return;
}
char[] ops = newchar[] {'+', '-', '*'};
for (charop : ops) {
tmp.append(op);
formFormulars(res, tmp, numList, start + 1);
tmp.deleteCharAt(tmp.length() - 1);
}
tmp.delete(oriSize, tmp.length());
}
publiclongresult(Stringformular) {
intres = 0;
char[] ops = getOps(formular);
String[] nums = formular.split("[^0-9]"); // Will generate an empty "" in the front
if (nums.length == 1) returnLong.valueOf(nums[0]);
Stack<Long> stack = newStack<>();
stack.push(Long.valueOf(nums[0]));
for (inti = 1; i < nums.length; i++) {
if (ops[i - 1]== '+') {
stack.push(Long.valueOf(nums[i]));
} elseif (ops[i - 1]== '-') {
stack.push(-Long.valueOf(nums[i]));
} elseif (ops[i - 1]== '*') {
stack.push(stack.pop() * Long.valueOf(nums[i]));
}
}
while (!stack.isEmpty()) {
res += stack.pop();
}
returnres;
}
publicchar[] getOps(Stringformular) {
intsize = 0;
for (charc : formular.toCharArray()) {
if (c == '+' || c == '-' || c == '*') {
size++;
}
}
char[] res = newchar[size];
inti = 0;
for (charc : formular.toCharArray()) {
if (c == '+' || c == '-' || c == '*') {
res[i++] = c;
}
}
returnres;
}
publicstaticvoidmain(String[] args) {
ExpressionAddOperationa = newExpressionAddOperation();
// a.addOperators("3456237490",9191);
// a.addOperators("105", 5);
a.addOperators("2147483648",
-2147483648);
// System.out.println(a.result("10-5"));
}
}