-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_150.cpp
More file actions
28 lines (24 loc) · 885 Bytes
/
Copy pathLeetCode_150.cpp
File metadata and controls
28 lines (24 loc) · 885 Bytes
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
class Solution {
public:
int evalRPN(vector<string>& arr) {
/*the basics login is we have to conver this postfix to infix and while conversion
we have to peform calculation */
stack<int>st;
for(int i=0;i<arr.size();i++){
if(arr[i] !="+" && arr[i] != "-" && arr[i] != "*" && arr[i] != "/"){
st.push(stoi(arr[i]));
}
else{
int a1= st.top();st.pop();
int a2=st.top();st.pop();
int result = 0;
if (arr[i] == "+") result = a2 + a1;
else if (arr[i] == "-") result = a2 - a1;
else if (arr[i] == "*") result = a2 * a1;
else if (arr[i] == "/") result = a2 / a1;
st.push(result);
}
}
return st.top();
}
};