Longest Common Prefix

来源:互联网 发布:大作家自动写作软件 编辑:程序博客网 时间:2024/05/30 04:36
class Solution {public:    string longestCommonPrefix(vector<string> &strs) {        vector<char> res;        int sz=strs.size();        if(sz==0)        {            return "";        }        if(strs[0].size()==0)        {            return "";        }        char curc=strs[0][0];        int index=0;        while(index<strs[0].size())        {            curc=strs[0][index];            for(int i=0;i<sz;++i)            {                if(strs[i].size()==0)                {                    return "";                }                if(index>=strs[i].size())                {                    return string(res.begin(),res.end());                }                if(strs[i][index]!=curc)                {                    return string(res.begin(),res.end());                }            }            res.push_back(curc);            index++;        }        return string(res.begin(),res.end());    }};

0 0