1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public: int evalRPN(vector<string>& tokens) { stack<long long> st; for (int i = 0; i < tokens.size(); i++) { if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
long long num1 = st.top(); st.pop(); long long num2 = st.top(); st.pop(); long long result = 0;
if(tokens[i] == "+") result = num2 + num1; if(tokens[i] == "-") result = num2 - num1; if(tokens[i] == "*") result = num2 * num1; if(tokens[i] == "/") result = num2 / num1; st.push(result); }else{ st.push(stoll(tokens[i])); } } return st.top(); } };
|