-
Notifications
You must be signed in to change notification settings - Fork 1
/
241 Different Ways to Add Parentheses .cpp
58 lines (58 loc) · 1.7 KB
/
241 Different Ways to Add Parentheses .cpp
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
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> nums;
vector<char> ops;
for (int i = 0; i < input.size(); i++) {
char c = input[i];
if (isdigit(c)) {
int num = 0;
int j;
for (j = i; j < input.size() && isdigit(input[j]); j++) {
num *= 10;
num += input[j] - '0';
}
nums.push_back(num);
i = j - 1;
} else {
ops.push_back(c);
}
}
auto calc = [] (int x, char op, int y) {
switch(op) {
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
}
return 0;
};
auto n = nums.size();
vector<vector<vector<int>>> f(n, vector<vector<int>>(n));
function<void(int, int)> dfs = [&] (int l, int r) {
auto &res = f[l][r];
if (res.size()) {
return ;
}
if (l == r) {
res = vector<int>{nums[l]};
return ;
}
for (int i = l; i < r; i++) {
dfs(l, i);
dfs(i + 1, r);
auto &left = f[l][i], &right = f[i + 1][r];
for (auto leftres : left) {
for (auto rightres : right) {
res.push_back(calc(leftres, ops[i], rightres));
}
}
}
return ;
};
dfs(0, n - 1);
return f[0][n - 1];
}
};