美文网首页
Longest Common Prefix

Longest Common Prefix

作者: 无云清晨 | 来源:发表于2017-12-20 19:58 被阅读0次

    字符串数组的公共前缀

     string longestCommonPrefix(vector<string>& strs)
     {
    
       bool isMatch = true;
       string w;
       string word;
       if (strs.size()<=0) return word;
       for(int i = 1; i <= strs[0].size(); i++)
       {
         w = strs[0].substr(0, i);
         for(int j = 1; j < strs.size(); j++)
         {
           if(strs[j].size() < i || w != strs[j].substr(0,i))
           {
              isMatch = false;
              break;
           }
         }
    
         if(!isMatch)
         {
           return word;
         }
    
         word = w;
       }
    
       return word;
     }
    

    相关文章

      网友评论

          本文标题:Longest Common Prefix

          本文链接:https://www.haomeiwen.com/subject/ysfnwxtx.html