int evalRPN(vector<string>& tokens) {
int n = tokens.size();
stack<string> st;
set<string> s = { "+","-","*","/" };
int count = 0;
for (int i = 0; i < n; i++) {
if (s.find(tokens[i]) == s.end()) {
st.push(tokens[i]);
}
else {
int a = atoi(st.top().c_str());
st.pop();
int b= atoi(st.top().c_str());
st.pop();
if (tokens[i] == "+")
st.push(to_string(a + b));
else if (tokens[i] == "-")
st.push(to_string(b - a));
else if (tokens[i] == "*")
st.push(to_string(a * b));
else if (tokens[i] == "/")
st.push(to_string(b / a));
}
}
return atoi(st.top().c_str());
}
leetcode刷题笔记 150.逆波兰表达式求值【中等】
最新推荐文章于 2022-10-26 23:22:11 发布