字符串数组的公共前缀
string longestCommonPrefix(vector<string>& strs)
{
bool isMatch = true;
string w;
string word;
if (strs.size()<=0) return word;
for(int i = 1; i <= strs[0].size(); i++)
{
w = strs[0].substr(0, i);
for(int j = 1; j < strs.size(); j++)
{
if(strs[j].size() < i || w != strs[j].substr(0,i))
{
isMatch = false;
break;
}
}
if(!isMatch)
{
return word;
}
word = w;
}
return word;
}
网友评论