首页 > 代码库 > leetcode 14

leetcode 14

求公共前缀,两两比较即可

string the_common_prefix(const string &str1,const string &str2){    string str="";    for(int i=0,j=0;i<str1.size()&&j<str2.size();i++,j++)    {    if(str1[i]==str2[j])     str.push_back(str1[i]);    else break;    }    return str;}class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        if(strs.size()==0)return "";        string The_Str=strs[0];        for(int i=1;i<strs.size();i++)            The_Str=the_common_prefix(The_Str,strs[i]);            return The_Str;    }};

 

leetcode 14