LeetCode 14:最长公共前缀(Longest Common Prefix)解法汇总

文章目录

  • 我的解答
  • Official
    • Approach 1: Horizontal scanning
    • Approach 2: Vertical scanning
    • Approach 3: Divide and conquer
    • Approach 4: Binary search

更多LeetCode题解

我的解答

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

Official

官方解答讲的很详细了,只不过它给的都是java,我改写成了C++。
英文版:https://leetcode.com/problems/longest-common-prefix/solution/
中文版:https://leetcode-cn.com/problems/longest-common-prefix/solution/

Approach 1: Horizontal scanning

class Solution {
public:
	string longestCommonPrefix(vector<string>& strs) {
		if (strs.empty()) { return ""; }
		string prefix = strs[0];
		for (int i = 1; i < strs.size(); i++) {
			while (strs[i].find(prefix)) {
				prefix = prefix.substr(0, prefix.size() - 1);
			}
		}
		return prefix;
	}
};

Approach 2: Vertical scanning

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

这种方法的思想和我的方法一样,但代码更简洁。

Approach 3: Divide and conquer

class Solution {
public:
	string longestCommonPrefix(vector<string>& strs) {
		if (strs.empty()) { return ""; }
		return longestCommonPrefix(strs, 0, strs.size() - 1);
	}
	string longestCommonPrefix(vector<string>& strs, int l, int r) {
		if (l == r) { return strs[l]; }
		else {
			int mid = (l + r) / 2;
			string lcpLeft = longestCommonPrefix(strs, l, mid);
			string lcpRight = longestCommonPrefix(strs, mid + 1, r);
			return commonPrefix(lcpLeft, lcpRight);
		}
	}
	string commonPrefix(string& lcpLeft, string& lcpRight) {
		for (int i = 0; i < lcpLeft.size(); i++) {
			if (!lcpRight[i] || lcpLeft[i] != lcpRight[i]) {
				return lcpLeft.substr(0, i);
			}
		}
		return lcpLeft;
	}
};

Approach 4: Binary search

class Solution {
public:
	string longestCommonPrefix(vector<string>& strs) {
		if (strs.empty()) { return ""; }
		int minLen = strs[0].size();
		for (string str : strs) {
			if (str.size() < minLen) {
				minLen = str.size();
			}
		}
		int low = 0, high = minLen;
		while (low <= high) {
			int mid = (low + high) / 2;
			if (isCommonPrefix(strs, mid)) { low = mid + 1; }
			else { high = mid - 1; }
		}
		return strs[0].substr(0, (low + high) / 2);
	}
	bool isCommonPrefix(vector<string>& strs, int mid) {
		string prefix = strs[0].substr(0, mid);
		for (int i = 1; i < strs.size(); i++) {
			if (strs[i].find(prefix)) { return false; }
		}
		return true;
	}
};

你可能感兴趣的:(LeetCode刷题题解记录)