您的位置:首页 > 其它

[LintCode] 最长公共前缀

2015-06-29 15:25 405 查看
class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string longestCommonPrefix(vector<string> &strs) {
// write your code here
string lcp = "";
if (strs.empty()) return lcp;
for (int i = 0; i < (int)strs[0].length(); i++) {
int pos = lcp.length();
char letter = strs[0][pos];
for (int j = 1; j < (int)strs.size(); j++)
if (strs[j].length() == pos || strs[j][pos] != letter)
return lcp;
lcp += letter;
}
return lcp;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: