-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150.cpp
40 lines (40 loc) · 1.14 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
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<string> st;
for (auto token : tokens) {
if (token == "/") {
string operand1 = st.top();
st.pop();
string operand2 = st.top();
st.pop();
int result = std::stoi(operand2) / std::stoi(operand1);
st.push(to_string(result));
} else if (token == "+") {
string operand1 = st.top();
st.pop();
string operand2 = st.top();
st.pop();
int result = std::stoi(operand2) + std::stoi(operand1);
st.push(to_string(result));
} else if (token == "*") {
string operand1 = st.top();
st.pop();
string operand2 = st.top();
st.pop();
int result = std::stoi(operand2) * std::stoi(operand1);
st.push(to_string(result));
} else if (token == "-") {
string operand1 = st.top();
st.pop();
string operand2 = st.top();
st.pop();
int result = std::stoi(operand2) - std::stoi(operand1);
st.push(to_string(result));
} else {
st.push(token);
}
}
return std::stoi(st.top());
}
};