Leetcode 14. 最长公共前缀

水题~

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string ans;
        if (strs.size() == 0) return ans;
        int k = 0;
        while (1) {
            if (k >= strs[0].size()) return ans;
            char e = strs[0][k];
            for (int i = 1; i < strs.size(); ++i)
                if (k >= strs[i].size() || strs[i][k] != e) return ans;
            ans += e, ++k;
        }
    }
};

你可能感兴趣的:(Leetcode 14. 最长公共前缀)