您的位置:首页 > 其它

14. Longest Common Prefix

2016-04-22 10:46 225 查看
Write a function to find the longest common prefix string amongst an array of strings.

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result;
int len = strs.size();
if(len==0) return result;
int len1 = strs[0].size();
for(int i = 0; i < len1; i++)
{
int count = 0;
for(int j = 0; j < len; j++)
{
if(strs[0][i] == strs[j][i])
count ++;
if(strs[0][i] != strs[j][i])
return result;
}
if(count== len)
result += strs[0][i];
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: