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;
    }

 

相关文章:

  • 2021-07-28
  • 2021-12-16
  • 2021-09-20
  • 2021-04-30
  • 2022-03-07
  • 2021-06-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-05
  • 2021-08-31
  • 2022-02-04
  • 2021-06-30
  • 2021-04-02
  • 2021-06-01
相关资源
相似解决方案