Wrong answer with Output value the same with Expected value???


  • 0
    F

    [""]
    Output:
    ""
    Expected:
    ""

    This is coming from below code:

    string longestCommonPrefix(vector<string>& strs) {
    		string result = "";
    		int len = strs.size();
    		if (len == 0) { return result; }
    
    		for (int i = 0; i < len; i++)
    		{
    			char c = strs[0][i];
    			for (int j = 1; j < len; j++)
    			{
    				if (strs[j][i] != c) { return result; }
    			}
    			result.push_back(c);
    		}
    		return result;
    }

  • 0
    S

    i know why , you put a '' to a result, in this case, the lenght of result is 1, not zero.


Log in to reply
 

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