美文网首页Leetcode
LeetCode #14 最长公共前缀

LeetCode #14 最长公共前缀

作者: HU兔兔 | 来源:发表于2020-02-07 13:40 被阅读0次
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.size()==0){
            return "";
        }
        string ans;
        int i=0;
        int j;
        while(i<strs[0].size()){
            j=1;
            while(j<strs.size()){
                if(i>=strs[j].size()||strs[j][i]!=strs[0][i]){
                    return ans;
                }
                j++;
            }
            ans.push_back(strs[0][i]);
            i++;
        }
        return ans;
    }
};

相关文章

网友评论

    本文标题:LeetCode #14 最长公共前缀

    本文链接:https://www.haomeiwen.com/subject/dgumxhtx.html