Timeline
Timeline
2025-11-19
init
Stack
Problem:
Classic problem. Here, since the problem guarantees the reverse Polish expression is valid, no validation is performed.
1234567891011121314151617181920212223242526272829303132333435 | using std::vector;using std::string;using std::stack;class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> st; int val1, val2, res = 0; for (string &token : tokens) { if (!token.compare("+") || !token.compare("-") || !token.compare("*") || !token.compare("/")) { val1 = st.top(); st.pop(); val2 = st.top(); st.pop(); if (!token.compare("+")) st.push(val2 + val1); else if (!token.compare("-")) st.push(val2 - val1); else if (!token.compare("*")) st.push(val2 * val1); else if (!token.compare("/")) st.push(val2 / val1); } else { st.push(std::stoi(token)); } } return st.top(); }}; |
