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


更多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;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值