Why did I get wrong answer ?


  • 0
    J
    Submission Result: Wrong Answer
    
    Input:         ["c","c"]
    Output:        "c"
    Expected:      "c"
    

    My code:

    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            if (strs.size() == 0) return ""; 
            if (strs.size() == 1) return strs[0];
            for (int i = 0; i < strs.size(); ++i) {
                if (strs[i].size() == 0) return "";
            }
            string common = "";
            int i = 0;
            for (; ;++i) {
                for (int j = 1; j < strs.size(); ++j) {
                    if (strs[j][i] != strs[0][i]) return common;
                }
                common += strs[0][i];
            }
            return common;
        }
    };

Log in to reply
 

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