-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path241.cpp
More file actions
58 lines (56 loc) · 1.35 KB
/
Copy path241.cpp
File metadata and controls
58 lines (56 loc) · 1.35 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
#include "common.h"
class Solution {
public:
vector<int> diffWaysToCompute(string exp) { return func(exp, 0, exp.size() - 1); }
vector<int> func(string exp, int s, int e) {
auto iter = dp.find({s, e});
if (iter != dp.end()) {
return iter->second;
}
vector<int> rst;
bool is_num = true;
for (auto i = s; i <= e; ++i) {
vector<int> left, right;
if (!isdigit(exp[i])) {
left = func(exp, s, i - 1);
right = func(exp, i + 1, e);
is_num = false;
}
if (exp[i] == '+') {
for (const auto& l : left) {
for (const auto& r : right) {
rst.push_back(l + r);
}
}
}
if (exp[i] == '-') {
for (const auto& l : left) {
for (const auto& r : right) {
rst.push_back(l - r);
}
}
}
if (exp[i] == '*') {
for (const auto& l : left) {
for (const auto& r : right) {
rst.push_back(l * r);
}
}
}
}
if (is_num) {
rst.push_back(atoi(exp.substr(s, e - s + 1).c_str()));
}
dp[{s, e}] = rst;
return rst;
}
map<pair<int, int>, vector<int>> dp;
};
int main() {
Solution s;
// auto rst = s.diffWaysToCompute("2-1-1");
auto rst = s.diffWaysToCompute("2*3-4*5");
for (const auto& item : rst) {
cout << item << endl;
}
}