讨论/《数组和字符串》 - 最长公共前缀/
《数组和字符串》 - 最长公共前缀
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string res = "";
		if (strs.size() == 0) return res;
		int end = strs[0].size() - 1;
		for (int i = 1; i < strs.size(); i++) {
			int j = 0;
			for (; j <= end&&j < strs[i].length(); j++) {
				if (strs[0][j] != strs[i][j]) break;
			}
			if (j-1 < end) end = j-1;
			if (end < 0) break;
		}
		for (int l = 0; l <= end; l++) {
			res += strs[0][l];
		}
		return res;
    }
};
展开全部 28 讨论