-
Notifications
You must be signed in to change notification settings - Fork 119
/
241.cpp
48 lines (41 loc) · 1.28 KB
/
241.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> result;
for (int k = 0; k < input.length(); k++) {
if (input[k] == '*' || input[k] == '-' || input[k] == '+') {
vector<int> a = diffWaysToCompute(input.substr(0, k));
vector<int> b = diffWaysToCompute(input.substr(k + 1));
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (input[k] == '+') {
result.push_back(a[i] + b[j]);
}
else if (input[k] == '-') {
result.push_back(a[i] - b[j]);
}
else {
result.push_back(a[i] * b[j]);
}
}
}
}
}
if (result.size() == 0) {
result.push_back(stoi(input));
}
return result;
}
};
int main() {
Solution s;
vector<int> ans = s.diffWaysToCompute("2*3-4*5");
for (int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}