1. 程式人生 > >LeetCode——最長公共字首

LeetCode——最長公共字首

string longestCommonPrefix(vector<string>& strs) {
        int count = 0;
        string newstring = "";
        if (strs.empty())
            return "";
        if(strs.size() == 1)
            return strs[0];
        while(1)
        {
            int num = 0;
            for (auto item = strs.begin(); item != strs.end(); ++item)
            {
                if((*item)[count] != (*strs.begin())[count])
                    return newstring;
                else
                    num++;
                if(num == strs.size())
                {
                    newstring += (*item)[count];
                    count++;    
                }
            }
        }
        return newstring;
    }