点击打开链接
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
int size = tokens.size();
for(int i=0;i<size;i++){
string str = tokens[i];
int first=0, second =0,val=0;
if(str=="+"){
second = st.top(); st.pop();
first = st.top(); st.pop();
st.push(first+second);
}
else if(str=="-"){
second = st.top(); st.pop();
first = st.top(); st.pop();
st.push(first-second);
}
else if(str=="*"){
second = st.top(); st.pop();
first = st.top(); st.pop();
st.push(first*second);
}
else if(str=="/"){
second = st.top(); st.pop();
first = st.top(); st.pop();
st.push(first/second);
}
else{
val = atoi(str.c_str()); //string先转换为字符串,再转换为int
st.push(val);
}
}
return st.top();
}
};
int main()
{
return 0;
}