class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string getCommonPrefix(string source,string target)
{
int i=0;
string comPrefix;
if(source.size()==0||target.size()==0)
return "";
while(source[i]==target[i])
i++;
comPrefix=source.substr(0,i);
return comPrefix;
}
string longestCommonPrefix(vector<string> &strs) {
// write your code here
int i;
if(strs.size()==0)
return "";
string comPrefix=strs[0];
for(i=1;i<strs.size();i++)
comPrefix=getCommonPrefix(comPrefix,strs[i]);
return comPrefix;
}
};
最长公共前缀
最新推荐文章于 2023-05-18 20:46:55 发布