程式語言 - LeetCode - C++ - 14. Longest Common Prefix



參考資訊:
https://algo.monster/liteproblems/14

題目:


方法:
使用第一個字串當作比較基準即可

解答:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int size = strs.size();

        for (int i = 0; i < strs[0].size(); ++i) {
            for (int j = 1; j < size; ++j) {
                if ((strs[j].size() <= i) || (strs[j][i] != strs[0][i])) {
                    return strs[0].substr(0, i);
                }
            }
        }

        return strs[0];
    }
};