8ms my C++ solution


  • 0
    J
    class Solution {
    public:
    	string findtwoStringPrefix(string a,string b){
    		string result;
    		if (a.size() <= 0 || b.size() <= 0)return "";
    		int i=0;
    		while(1){
               if(a[i]==b[i])
               	  i++;
               else break;
    		}
    		result = a.substr(0,i);
    	    return result;
    	}
        void longestCommonPrefix2(vector<string>& strs,int begin,int end,string& result) 
        {
                if(begin>=end){
                    result =strs[begin];
                    return;
                }
        	    int mid = (begin+end)/2;
        	    string a=result;
        	    string b=result;
                longestCommonPrefix2(strs,begin,mid,a);
                longestCommonPrefix2(strs,mid+1,end,b);
                string c = findtwoStringPrefix(a,b);
                if(c.size()>result.size()){
                    result = c;
                }
    
        }
       
        string longestCommonPrefix(vector<string>& strs) {
              string result="";
              if(strs.size()<=0)return result;
              longestCommonPrefix2(strs,0,strs.size()-1,result);
              return result;
                
        }
    };

Log in to reply
 

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