-
Notifications
You must be signed in to change notification settings - Fork 0
/
200213-1.cpp
53 lines (51 loc) · 1.41 KB
/
200213-1.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
// https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
vector<int> stack;
for (int i = 0; i < tokens.size(); ++i) {
const string& s = tokens[i];
if (s == "+") {
int b = stack.back(); stack.pop_back();
int a = stack.back(); stack.pop_back();
stack.push_back(a + b);
} else if (s == "-") {
int b = stack.back(); stack.pop_back();
int a = stack.back(); stack.pop_back();
stack.push_back(a - b);
} else if (s == "*") {
int b = stack.back(); stack.pop_back();
int a = stack.back(); stack.pop_back();
stack.push_back(a * b);
} else if (s == "/") {
int b = stack.back(); stack.pop_back();
int a = stack.back(); stack.pop_back();
stack.push_back(a / b);
} else {
stack.push_back(stoi(s));
}
}
return stack.back();
}
};
int main()
{
Solution s;
{
vector<string> a = {"2", "1", "+", "3", "*"};
printf("%d\n", s.evalRPN(a)); // answer: 9 - explain: ((2 + 1) * 3) = 9
}
{
vector<string> a = {"4", "13", "5", "/", "+"};
printf("%d\n", s.evalRPN(a)); // answer: 6 - explain: (4 + (13 / 5)) = 6
}
{
vector<string> a = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
printf("%d\n", s.evalRPN(a)); // answer: 22 - explain: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
}
return 0;
}