-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.evalRPN.cpp
More file actions
executable file
·67 lines (62 loc) · 1.71 KB
/
Copy path36.evalRPN.cpp
File metadata and controls
executable file
·67 lines (62 loc) · 1.71 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
/*
@filename 36.cpp
@author caonan
@date 2022-04-11 13:19:32
@reference 剑指offer专项
@url https://leetcode-cn.com/problems/8Zf90G/
@brief 根据 逆波兰表示法,求该后缀表达式的计算结果。
有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
说明:
整数除法只保留整数部分。
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0
的情况。
*/
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> stack_store;
for (const auto &str : tokens) {
if (str == "+" || str == "-" || str == "*" || str == "/") {
int num2 = stack_store.top();
stack_store.pop();
int num1 = stack_store.top();
stack_store.pop();
switch (str[0]) {
case '+':
stack_store.emplace(num1 + num2);
break;
case '-':
stack_store.emplace(num1 - num2);
break;
case '*':
stack_store.emplace(num1 * num2);
break;
case '/':
assert(num2 != 0);
stack_store.emplace(num1 / num2);
break;
default:
break;
}
} else {
stack_store.emplace(stoi(str));
}
}
return stack_store.top();
}
};
int main() {
vector<string> in{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
Solution s;
int ret = s.evalRPN(in);
cout << ret << endl;
return 0;
}