My cpp solution of 7ms


  • 0
    S

    class Solution {

    public:

    string longestCommonPrefix(vector<string> &strs) {

    string answer="";

    int i,j;

    int leastlength=INT_MAX;

    for(i=0;i < strs.size();i++)



    if(leastlength>strs[i].size())

    leastlength=strs[i].size();


        for(j=0;j<leastlength;j++)
           {
            for(i=1;i<strs.size();i++)
               {
                if(strs[i-1][j]!=strs[i][j])
                    break;
                }
            if(i==strs.size())
            answer+=strs[i-1][j];
            else 
            break;
           }
            
        return answer;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.