leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0150.cpp (769B)


      1 class Solution {
      2     bool is_op(const string &s) { return s == "+" || s == "-" || s == "*" || s == "/"; }
      3 
      4   public:
      5     int evalRPN(vector<string> &tokens) {
      6         if (tokens.size() == 0) return 0;
      7 
      8         stack<long long> st;
      9         for (string &s : tokens) {
     10             if (is_op(s)) {
     11                 long long y = st.top();
     12                 st.pop();
     13                 long long x = st.top();
     14                 st.pop();
     15                 switch (s[0]) {
     16                 case '+': st.push(x + y); break;
     17                 case '-': st.push(x - y); break;
     18                 case '*': st.push(x * y); break;
     19                 case '/': st.push(x / y); break;
     20                 }
     21             } else
     22                 st.push(stoi(s));
     23         }
     24         return st.top();
     25     }
     26 };