写过最简单的一道最长公共前缀题,但是解法比较多
题目:

解题思路:
一、纵向扫描:
看了下官方题解,我这属于纵向扫描。
纵向扫描:从前往后遍历所有字符串的每一列,比较相同列上的字符是否相同,如果相同则继续对下一列进行比较,如果不相同则当前列不再属于公共前缀,当前列之前的部分为最长公共前缀。
非常简单,直接附上我的代码:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int num=strs.size();
string max_str="";
int index=0;
int len=strs[0].length();
for(int i=0;i<len;i++){
for(int j=0;j<num;j++){
if(strs[0][i]!=strs[j][i]){
return max_str;
}
}
max_str+=strs[0][i];
}
return max_str;
}
};
二、横向扫描
原理是遍历字符串数组中的每个字符串,不断地更新最长公共前缀,当遍历完所有的字符串以后,即可得到字符串数组中的最长公共前缀。
感觉有点麻烦。。。时间复杂度和我写的一样都是O(mn)
不过,如果在尚未遍历完所有的字符串时,最长公共前缀已经是空串,则最长公共前缀一定是空串,因此不需要继续遍历剩下的字符串,直接返回空串即可。
直接附上官方题解:
思路是下面这个函数不断遍历两个字符串,得到两个字符串的最长公共前缀,返回给上面这个函数,上面这个函数不断比较下一个字符串和当前得到的最长公共前缀。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (!strs.size()) {
return "";
}
string prefix = strs[0];
int count = strs.size();
for (int i = 1; i < count; ++i) {
prefix = longestCommonPrefix(prefix, strs[i]);
if (!prefix.size()) {
break;
}
}
return prefix;
}
string longestCommonPrefix(const string& str1, const string& str2) {
int length = min(str1.size(), str2.size());
int index = 0;
while (index < length && str1[index] == str2[index]) {
++index;
}
return str1.substr(0, index);
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/solutions/288575/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
三、分治
思路是将字符串数组不断划分成更小的两个子集,找到两个子集的最长公共前缀。
主体是第二个和第三个函数,如果start==end,说明字符串数组只有一个字符串,也就是说分治到了最底层,只有一个字符串,直接作为答案向回传。
不然的话,说明还没到最底层,继续分治。
将得到的左子树和右子树的答案进行一个比较,遍历直到不相等。
利用函数substr(a,b)得到位置从(a,b)的子串
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (!strs.size()) {
return "";
}
else {
return longestCommonPrefix(strs, 0, strs.size() - 1);
}
}
string longestCommonPrefix(const vector<string>& strs, int start, int end) {
if (start == end) {
return strs[start];
}
else {
int mid = (start + end) / 2;
string lcpLeft = longestCommonPrefix(strs, start, mid);
string lcpRight = longestCommonPrefix(strs, mid + 1, end);
return commonPrefix(lcpLeft, lcpRight);
}
}
string commonPrefix(const string& lcpLeft, const string& lcpRight) {
int minLength = min(lcpLeft.size(), lcpRight.size());
for (int i = 0; i < minLength; ++i) {
if (lcpLeft[i] != lcpRight[i]) {
return lcpLeft.substr(0, i);
}
}
return lcpLeft.substr(0, minLength);
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/solutions/288575/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
四、二分查找
首先,找到字符串数组中最短的字符串,也就是说最长公共前缀一定小于等于这个最短字符串。
利用二分查找不断地试错这个最短字符串,看取多长最合适:
(1)如果长度为mid的前缀是公共前缀,则去右半部分查找更长的可能前缀;
(2)如果不是,则去左半部分查找更短的可能前缀
接着,以第一个字符串的前length个字符为基准,逐个检查其他字符串的前length个字符是否与之完全匹配,只要有一个字符不匹配,就返回false,否则最终返回true。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (!strs.size()) {
return "";
}
int minLength = min_element(strs.begin(), strs.end(), [](const string& s, const string& t) {return s.size() < t.size();})->size();
int low = 0, high = minLength;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (isCommonPrefix(strs, mid)) {
low = mid;
}
else {
high = mid - 1;
}
}
return strs[0].substr(0, low);
}
bool isCommonPrefix(const vector<string>& strs, int length) {
string str0 = strs[0].substr(0, length);
int count = strs.size();
for (int i = 1; i < count; ++i) {
string str = strs[i];
for (int j = 0; j < length; ++j) {
if (str0[j] != str[j]) {
return false;
}
}
}
return true;
}
};
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/solutions/288575/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间复杂度:O(mnlogm)
m:字符串数组中的字符串的最小长度
n:字符串的数量。
二分查找的迭代执行次数是 O(logm),每次迭代最多需要比较 mn 个字符,因此总时间复杂度是 O(mnlogm)。

被折叠的 条评论
为什么被折叠?



