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 longestCommonPrefix(vector<string>& strs) {
    if (empty(strs)) {
      return "";
    }
    string s{strs[0]};
    for (int i = 1; i < size(strs); ++i) {
      const string& x = strs[i];
      int cur = 0;
      while (s[cur] == x[cur]) {
        ++cur;
        if (cur == size(s) || cur == size(x)) {
          break;
        }
      }
      s = s.substr(0, cur);
    }
    return s;
  }
};