LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  int evalRPN(vector<string>& tokens) {
    stack<int> s;
    for (auto& x : tokens) {
      if (x == "+" || x == "-" || x == "*" || x == "/") {
        int b = s.top();
        s.pop();
        int a = s.top();
        s.pop();
        if (x == "+") {
          s.emplace(a + b);
        } else if (x == "-") {
          s.emplace(a - b);
        } else if (x == "*") {
          s.emplace(a * b);
        } else {
          s.emplace(a / b);
        }
      } else {
        s.emplace(stoi(x));
      }
    }
    return s.top();
  }
};