-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluate Reverse Polish Notation.h
More file actions
40 lines (39 loc) · 1.11 KB
/
Evaluate Reverse Polish Notation.h
File metadata and controls
40 lines (39 loc) · 1.11 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
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> st;
for(int i = 0; i < tokens.size(); ++i) {
string s = tokens[i];
if(s == "+") {
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num1+num2);
} else if (s == "-") {
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num2-num1);
} else if (s == "*") {
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num1*num2);
} else if (s == "/") {
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
st.push(num2/num1);
} else {
int num;
istringstream(s) >> num;
st.push(num);
}
}
return st.top();
}
};