-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc241
28 lines (26 loc) · 1018 Bytes
/
lc241
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
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> res;
for(int i = 0; i < input.size();i++) {
if(input[i] == '+' || input[i] == '-' || input[i] == '*') {
vector<int> left = diffWaysToCompute(input.substr(0,i));
vector<int> right = diffWaysToCompute(input.substr(i + 1));
for(int j = 0; j < right.size();j++) {
for(int k = 0; k < left.size();k++) {
if(input[i] == '+') {
res.push_back(left[k] + right[j]);
} else if(input[i] == '-') {
res.push_back(left[k] - right[j]);
} else {
res.push_back(left[k] * right[j]);
}
}
}
}
}
if(res.empty())
res.push_back(stoi(input));
return res;
}
};