forked from MaskRay/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferent-ways-to-add-parentheses.cc
More file actions
38 lines (37 loc) · 1 KB
/
different-ways-to-add-parentheses.cc
File metadata and controls
38 lines (37 loc) · 1 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
// Different Ways to Add Parentheses
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> nums;
vector<char> ops;
for (int j = 0, i = 0; i < input.size(); i = j) {
int x = 0;
for (; j < input.size() && isdigit(input[j]); j++)
x = x*10+input[j]-'0';
nums.push_back(x);
if (j < input.size())
ops.push_back(input[j++]);
}
int n = nums.size();
vector<vector<vector<int>>> s(n, vector<vector<int>>(n));
REP(i, n)
s[i][i].push_back(nums[i]);
ROF(i, 0, n)
FOR(j, i+1, n)
FOR(k, i, j)
for (auto x: s[i][k])
for (auto y: s[k+1][j])
s[i][j].push_back(op(ops[k], x, y));
return s[0][n-1];
}
int op(char c, int x, int y) {
switch (c) {
case '+': return x+y;
case '-': return x-y;
default: return x*y;
}
}
};