-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffWaysToCompute.cpp~
71 lines (66 loc) · 1.68 KB
/
diffWaysToCompute.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
#include <vector>
#include <string>
#include <cstdlib>
#include <unordered_map>
using std::string;
using std::vector;
using std::atoi;
using std::ordered_map;
/**
* Method 1:
* use the recursive to compute
*/
vector<int> diffWaysToCompute1(string input){
int size = input.size();
vector<int> result;
for(int i = 0; i < size; i++){
char ch = input[i];
vector<int> left, right;
if(ch == '+' || ch == '-' || ch == '*'){
left = diffWaysToCompute(input.substr(0, i));
right = diffWaysToCompute(input.substr(i+1));
for(auto n1 : left){
for(auto n2 : right){
if(ch == '+') result.push_back(n1 + n2);
else if(ch == '-') result.push_back(n1 - n2);
else result.push_back(n1 * n2);
}
}
}
}
if(result.size() == 0) result.push_back(atoi(input.c_str());
return result;
}
vector<int> diffWaysToCompute2(string input){
unordered_map<string, vector<int>> map;
return compute(input, map);
}
vector<int> compute(string str, unordered_map<string, vector<int>>& map){
vector<int> result;
for(int i = 0; i < str.size(); i++){
char ch = str[i];
vector<int> left, right;
if(ch == '+' || ch == '-' || ch == '*'){
string str1 = str.substr(0, i);
string str2 = str.substr(i + 1);
if(map.find(str1) != map.end())
left = map[str1];
else
left = compute(str1, map);
if(map.find(str2) != map.end())
right = map[str2];
else
right = compute(str2, map);
for(auto n1 : left){
for(auto n2 : right){
if(ch == '+') result.push_back(n1 + n2);
else if(ch == '-') result.push_back(n1 - n2);
else result.push_back(n1 * n2);
}
}
}
}
if(result.empty()) result.push_back(atoi(str.c_str()));
map[str] = result;
return result;
}