题目描述:
Write a function to find the longest common prefix string amongst an array of strings.
代码:
string Solution::longestCommonPrefix(vector<string> &strs)
{
string result = "";
if(strs.size() == 0)
return result;
if(strs.size() == 1)
return strs[0];
for(int i = 0;i < strs[0].size();i++)
{
char c = strs[0][i];
for(int j = 1;j < strs.size();j++)
if(strs[j-1][i] != strs[j][i])
return result;
result = result + c;
}
return result;
}LeetCode:Longest Common Prefix
原文地址:http://blog.csdn.net/yao_wust/article/details/42454773