最长公共前缀

来源:互联网 发布:詹姆斯选秀体测数据 编辑:程序博客网 时间:2024/06/14 02:43

lintcode最长公共前缀

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;    }};
0 0
原创粉丝点击