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:
  bool isValid(string s) {
    stack<char> stk;
    for (auto& x : s) {
      if (x == '(' || x == '{' || x == '[') {
        stk.emplace(x);
      } else if (!empty(stk) && stk.top() == m.at(x)) {
        stk.pop();
      } else {
        return false;
      }
    }
    return empty(stk);
  }

 private:
  const unordered_map<char, char> m{
      {')', '('},
      {'}', '{'},
      {']', '['},
  };
};