Write a function to find the longest common prefix string amongst an array of strings.
求若干串字符串的最长共同前缀子串。直接每个字符串对比即可得到结果。
代码:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs)
{
string res;
if(strs.empty()) return res;
int n=strs.size();
res=strs[0];
int r=res.size();
for(int i=1;i<n;i++)
{
for(int j=0;j<r;j++)
{
if(res[j]!=strs[i][j])
{
r=j;
break;
}
}
}
return res.substr(0,r);
}
};