leetcode 14 Longest Common Prefix

2018-06-04 11:50:54 浏览数 (1)

代码语言:javascript复制
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty()) return "";
        for (int j = 0; j < strs[0].size();   j) {
            for (int i = 0; i < strs.size() - 1;   i) {
                if (j >= strs[i].size() || j >= strs[i   1].size() || strs[i][j] != strs[i   1][j]) {
                    return strs[i].substr(0, j);
                }
            }
        }
        return strs[0];
    }
};

0 人点赞