A 4 ms C++ solution


  • 0
    string longestCommonPrefix(vector<string>& strs) 
    {
    	string res;
    	int sz = strs.size();
    	if (0 == sz)
    		return res;
    	int i, j;
    	char t;
    	for (i = 0; i < strs[0].size(); ++i)
    	{
    		t = strs[0][i];
    		for (j = 1; j < sz; ++j)
    		{
    			if (i >= strs[j].size() || t - strs[j][i])
    				return res;
    		}
    		res.push_back(t);
    	}
    	return res;
    }

Log in to reply
 

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