1. 整体思路,定义数据栈存放数据, 只要遇到运算符就出栈两个数做加减乘除处理.
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i=0;i<tokens.size();i++) {
if(tokens[i]!="+" && tokens[i]!="-" && tokens[i]!="*" && tokens[i]!="/") {
int num = stoi(tokens[i]);
st.push(num);
}
else {
int second = st.top(); st.pop();
int first = st.top(); st.pop();
int all = 0;
if(tokens[i]=="+") all = first+second;
if(tokens[i]=="-") all = first-second;
if(tokens[i]=="*") all = first*second;
if(tokens[i]=="/") all = first/second;
st.push(all);
}
}
return st.top();
}
};