编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
注意此题指最长公共前缀,表示从第一位开始,不要把此题想得太难了。
Mycode:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.empty())return "";
string ans=strs[0];
for(int i=1;i<strs.size();i++)
{
ans=findans(ans,strs[i]);
}
return ans;
}
string findans(string a,string b)
{
int len=min(a.size(),b.size());
int j=0;
for(;j<len;j++)
{
if(a[j]!=b[j]) break;
else if(a[j]==b[j])continue;
}
return a.substr(0,j);
}
};
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string res = strs.empty() ? "" : strs[0];
for (string s : strs)
while (s.find(res) != 0) res = res.substr(0, res.length() - 1); //substr拷贝指定位数的字符串内容
return res;
}
};
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);
}
};