-
Notifications
You must be signed in to change notification settings - Fork 1
/
150.cpp
42 lines (37 loc) · 1.12 KB
/
150.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
// 150. Evaluate Reverse Polish Notation - https://leetcode.com/problems/evaluate-reverse-polish-notation
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
set<string> ops = {"+", "-", "*", "/"};
stack<int> st;
for (const auto & token : tokens) {
if (ops.count(token)) {
int op1 = st.top(); st.pop();
int op2 = st.top(); st.pop();
switch (token.front()) {
case '+':
st.emplace(op2 + op1);
break;
case '-':
st.emplace(op2 - op1);
break;
case '*':
st.emplace(op2 * op1);
break;
case '/':
st.emplace(op2 / op1);
break;
}
} else {
st.emplace(stoi(token));
}
}
return st.top();
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}