Longest Common Prefix

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

你可能感兴趣的:(leetcode)