LeetCode 14 Longest Common Prefix

题目

c++

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        
        if(strs.size()==0)
            return "";
        int i=0;
        string ans="";
        while(true)
        {
            if(i==strs[0].length())
                return ans;
            char s=strs[0][i];
            for(int j=1;j<strs.size();j++)
            {
                if(i==strs[j].length())
                    return ans;
                if(strs[j][i]!=s)
                    return ans;
            }
            ans+=s;
            i++;
        }
        return ans;
    }
};
原文地址:https://www.cnblogs.com/dacc123/p/11067880.html