Friday, March 18, 2016

[LeetCode] 14. Longest Common Prefix My Submissions Question

class Solution {
public:
    string longestCommonPrefix(vector &strs) {
        string comPrefix;
        if(strs.empty()) return comPrefix;
        // It is better to compare with the first element instead of previous element.

        for(int i=0; i=strs[j].size() || strs[j][i]!=strs[0][i])
                    return comPrefix;
            }
            comPrefix.push_back(strs[0][i]);
        }
        return comPrefix;
    }
};

No comments:

Post a Comment