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:
  string simplifyPath(string path) {
    string res;
    vector<string> t{split(path, '/')};
    for (auto& x : t) {
      res += '/';
      res += x;
    }
    return empty(res) ? "/" : res;
  }

  vector<string> split(const string& s, char delemiter) {
    vector<string> res;
    istringstream ss{s};
    string t;
    while (getline(ss, t, delemiter)) {
      if (empty(t) || t == "." || (t == ".." && empty(res))) {
        continue;
      } else if (t == ".." && !empty(res)) {
        res.pop_back();
      } else {
        res.emplace_back(t);
      }
    }
    return res;
  }
};