-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferentWaystoAddParentheses.cc
More file actions
50 lines (47 loc) · 1.12 KB
/
Copy pathDifferentWaystoAddParentheses.cc
File metadata and controls
50 lines (47 loc) · 1.12 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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int compute(int a, int b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
}
return 1;
}
vector<int> diffWaysToCompute(string input) {
int val = 0, idx = 0;
while (idx < input.length() && isdigit(input[idx])) {
val *= 10;
val += input[idx++] - '0';
}
if (idx == input.length()) return {val};
vector<int> res;
vector<int> left, right;
for (int i = 0; i < input.length(); ++i) {
if (!isdigit(input[i])) {
left = diffWaysToCompute(input.substr(0, i));
right = diffWaysToCompute(input.substr(i + 1, input.length() - 1 - i));
for (int j = 0; j < left.size(); ++j) {
for (int k = 0; k < right.size(); ++k) {
res.push_back(compute(left[j], right[k], input[i]));
}
}
}
}
return res;
}
};
int main(int argc, char const *argv[]) {
Solution sol;
return 0;
}