leetcode string 14 longest common prefix

THE

problem

solution

Code


class Solution {
    
    
public:
   string longestCommonPrefix(vector<string>& strs) {
    
    
       
       string ans ="" ;
       if(!strs.size()) return ans;
       if(strs.size()==1) return strs[0];
       int len = strs.size();

       for(int i = 0 ; i<strs[0].size();i++){
    
    
           //cout<<strs[i];
           for(int j = 0 ; j<len;j++){
    
    
               if(strs[0][i]!=strs[j][i])return ans;
           }
           ans = ans +strs[0][i];


       }
       
   return ans;
   }
};

Summary and reflection

  1. Pay attention to consideration of boundary issues. Comparative problems are more basic. There is no combined algorithm.

Guess you like

Origin blog.csdn.net/liupeng19970119/article/details/114199192
Recommended