题目
Write a function to findthe longest common prefix string amongst an array of strings.
求最长公共前缀,从头开始扫,
注意数组为空,注意到字符串尾(尤其是同时到尾)
代码:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int size=strs.size();
string ans="";
if(size==0)
return ans;
if(size==1)
return strs[0];
int i=size,j=0;
while(i>=size)
{
for(i=1;i<size;i++)
{
if(strs[i][j]!=strs[0][j]||strs[i][j]=='\0')
break;
}
if(i>=size)
ans+=strs[0][j++];
}
return ans;
}
};