-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241.cpp
45 lines (45 loc) · 1.43 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
class Solution {
// Runtime: 0 ms Your runtime beats 100.00 % of cpp submissions
// Memory Usage: 9.1 MB Your memory usage beats 95.63 % of cpp submissions.
public:
vector<int> diffWaysToCompute(string input) {
unordered_map<string_view, vector<int>> dpMap;
function<void(string_view)> recursion = [&](string_view input) {
vector<int> result;
for (int i = 0; i < input.size(); i++) {
char cur = input[i];
if (cur == '+' || cur == '-' || cur == '*') {
auto substr = input.substr(0, i);
if (dpMap.find(substr) == dpMap.end())
recursion(substr);
auto &result1 = dpMap[substr];
substr = input.substr(i + 1);
if (dpMap.find(substr) == dpMap.end())
recursion(substr);
auto &result2 = dpMap[substr];
for (auto n1 : result1)
for (auto n2 : result2)
switch (cur) {
case '+':
result.push_back(n1 + n2);
break;
case '-':
result.push_back(n1 - n2);
break;
case '*':
result.push_back(n1 * n2);
break;
}
}
}
if (result.empty()) {
int res;
from_chars(input.data(), input.data() + input.size(), res);
result.push_back(res);
}
dpMap[input] = move(result);
};
recursion(input);
return dpMap[input];
}
};