[leetcode]最长公共前缀Longest Common Prefix

版权声明:转载请加上链接 https://blog.csdn.net/qq_29407397/article/details/90115480

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        
    if (strs.empty()) return "";
        string res = "";
        for (int j = 0; j < strs[0].size(); ++j) {
            char c = strs[0][j];
            for (int i = 1; i < strs.size(); ++i) {
                if (j >= strs[i].size() || strs[i][j] != c) {
                    return res;
                }
            }
            res.push_back(c);
        }
        return res;
        
    }
};

猜你喜欢

转载自blog.csdn.net/qq_29407397/article/details/90115480