forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
different-ways-to-add-parentheses.cpp
76 lines (73 loc) · 2.87 KB
/
different-ways-to-add-parentheses.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Time: O(n * (C(2n, n) - C(2n, n - 1))), this is at most
// Space: O(n * (C(2n, n) - C(2n, n - 1)))
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<vector<vector<int>>> lookup(input.length() + 1,
vector<vector<int>>(input.length() + 1));
return diffWaysToComputeRecu(input, 0, input.length(), lookup);
}
vector<int> diffWaysToComputeRecu(const string& input,
const int start, const int end,
vector<vector<vector<int>>>& lookup) {
if (!lookup[start][end].empty()) {
return lookup[start][end];
}
vector<int> result;
for (int i = start; i < end; ++i) {
const auto cur = input[i];
if (cur == '+' || cur == '-' || cur == '*') {
auto left = diffWaysToComputeRecu(input, start, i, lookup);
auto right = diffWaysToComputeRecu(input, i + 1, end, lookup);
for (const auto& num1 : left) {
for (const auto& num2 : right) {
if (cur == '+') {
result.emplace_back(num1 + num2);
} else if (cur == '-') {
result.emplace_back(num1 - num2);
} else {
result.emplace_back(num1 * num2);
}
}
}
}
}
// If the input string contains only number.
if (result.empty()) {
result.emplace_back(stoi(input.substr(start, end - start)));
}
lookup[start][end] = result;
return lookup[start][end];
}
};
// Time: O(n * (C(2n, n) - C(2n, n - 1))), this is at least
// Space: O(C(2n, n) - C(2n, n - 1))
class Solution2 {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> result;
for (int i = 0; i < input.length(); ++i) {
const auto cur = input[i];
if (cur == '+' || cur == '-' || cur == '*') {
auto left = diffWaysToCompute(input.substr(0, i));
auto right = diffWaysToCompute(input.substr(i + 1));
for (const auto& num1 : left) {
for (const auto& num2 : right) {
if (cur == '+') {
result.emplace_back(num1 + num2);
} else if (cur == '-') {
result.emplace_back(num1 - num2);
} else {
result.emplace_back(num1 * num2);
}
}
}
}
}
// If the input string contains only number.
if (result.empty()) {
result.emplace_back(stoi(input));
}
return result;
}
};