您的位置:首页 > 其它

[Leetcode] #14 Longest Common Prefix

2017-04-05 21:07 351 查看

Discription:

Write a function to find the longest common prefix string amongst an array of strings.

Solution:

string longestCommonPrefix(vector<string>& strs) {
if (strs.empty())
return "";
int len = strs[0].size();
for (string str : strs){
if (str.size() < len)
len = str.size();
}
string result = "";
for (int i = 0; i < len; i++){
for (int j = 0; j < strs.size()-1; j++){
if (strs[j][i] != strs[j + 1][i])
return result;
}
result += strs[0][i];
}
return result;
}
GitHub-Leetcode:https://github.com/wenwu313/LeetCode
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: