-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse_polish_notation.cpp
More file actions
87 lines (81 loc) · 2.44 KB
/
reverse_polish_notation.cpp
File metadata and controls
87 lines (81 loc) · 2.44 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <string>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/**
* Describe: Given an expression string array, return the Reverse Polish
* notation of this expression. (remove the parentheses)
*
* Solution:
* a. encounter ')' need reduce to next '(' in op stack
* b. encounter '+' or '-' need to reduce all op in the stack or encounter '('
* c. encounter '/' or '*' need to reduce all consecutive '*' or '/' in the
* stack.
* d. expression_a op expression_b
* ==> [convert(expression_a), expression_b, op]
*/
class Solution {
public:
vector<string> convertToRPN(vector<string> &expression) {
stack<vector<string>> numbers;
stack<string> ops;
for (auto ele : expression) {
if (ele == "(") {
ops.push(ele);
} else if (ele == ")") {
while (ops.top() != "(") {
merge(numbers, ops);
}
ops.pop();
} else if (ele == "+" || ele == "-") {
while (!ops.empty() && ops.top() != "(") {
merge(numbers, ops);
}
ops.push(ele);
} else if (ele == "*" || ele == "/") {
if (!ops.empty()) {
if (ops.top() == "/" || ops.top() == "*") {
merge(numbers, ops);
}
}
ops.push(ele);
} else {
numbers.push(vector<string>{ele});
}
}
while (!ops.empty()) {
merge(numbers, ops);
}
if (!numbers.empty()) {
return numbers.top();
}
return vector<string>();
}
void merge(stack<vector<string>> &numbers, stack<string> &ops) {
if (ops.empty()) {
return;
}
if (ops.top() == "(") {
return;
}
string op_top = ops.top();
ops.pop();
vector<string> &numbers_top = numbers.top();
numbers.pop();
numbers_top.push_back(op_top);
vector<string> &top = numbers.top();
top.insert(top.end(), numbers_top.begin(), numbers_top.end());
}
};
int main() {
Solution so;
vector<string> test{"2", "+", "3", "(", "5", "+", "(", "6", "*", "7", ")", ")"};
auto re = so.convertToRPN(test);
cout << "result: ";
for (auto& ele : re) {
cout << ele << " ";
}
cout << endl;
return 0;
}