leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0150.cpp (769B)
0 class Solution { 1 bool is_op(const string &s) { return s == "+" || s == "-" || s == "*" || s == "/"; } 2 3 public: 4 int evalRPN(vector<string> &tokens) { 5 if (tokens.size() == 0) return 0; 6 7 stack<long long> st; 8 for (string &s : tokens) { 9 if (is_op(s)) { 10 long long y = st.top(); 11 st.pop(); 12 long long x = st.top(); 13 st.pop(); 14 switch (s[0]) { 15 case '+': st.push(x + y); break; 16 case '-': st.push(x - y); break; 17 case '*': st.push(x * y); break; 18 case '/': st.push(x / y); break; 19 } 20 } else 21 st.push(stoi(s)); 22 } 23 return st.top(); 24 } 25 };