一、题目
二、解题思路
1、我的思路
乍一看我的代码量还是比较少,但是提交上去发现时间效率和空间效率都不占优势
讲讲我的思路:首先通过for循环找出数组中长度最短的字符串,并用min储存最短字符串的长度,最长公共前缀不可能比min更大。其次在通过双重循环逐个比较数组中字符串的每一位是否对应相等,若相等就往s中追加一个字母,若不相等则直接结束程序,返回s
String s = "";
int min = strs[0].length();
for (int i = 1; i < strs.length; i++){
if(strs[i].length() < min){
min = strs[i].length();
}
}
for (int j = 0; j < min; j++) {
for (int i = 1; i < strs.length; i++) {
if (strs[0].charAt(j) != strs[i].charAt(j)) {
return s;
}
}
s += strs[0].charAt(j);
}
return s;
2、官方题解
力扣提供了四种题解,我们一种一种来看(终于体会到一道算法题,一看一下午的感觉了)
方法一:横向扫描
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
int count = strs.length;
for (int i = 1; i < count; i++) {
prefix = longestCommonPrefix(prefix, strs[i]);
if (prefix.length() == 0) {
break;
}
}
return prefix;
}
public String longestCommonPrefix(String str1, String str2) {
int length = Math.min(str1.length(), str2.length());
int index = 0;
while (index < length && str1.charAt(index) == str2.charAt(index)) {
index++;
}
return str1.substring(0, index);
}
看完这段代码,脑袋里冒出两句话:层层套娃但是妙啊,可我也确实想不到啊
这种方法只能靠积累了,反正我是想不到的(而且我也解释不来,所以大家自己看吧)
方法二:纵向扫描
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int length = strs[0].length();
int count = strs.length;
for (int i = 0; i < length; i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < count; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这种方法和我的思路有异曲同工之妙,但我的方法更复杂一些
其实我一开始写的代码和官方题解挺像的,下面是我一开始提交的代码
class Solution {
public String longestCommonPrefix(String[] strs) {
String s = "";
for (int j = 0; j < strs[0].length(); j++) {
for (int i = 1; i < strs.length; i++) {
if (strs[0].charAt(j) != strs[i].charAt(j)) {
return s;
}
}
s += strs[0].charAt(j);
}
return s;
}
}
但是我没有考虑到i == strs[j].length()的情况,所以提交的时候代码执行出错了,还是思维不够严谨导致的
方法三:分治
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
} else {
return longestCommonPrefix(strs, 0, strs.length - 1);
}
}
public String longestCommonPrefix(String[] strs, int start, int end) {
if (start == end) {
return strs[start];
} else {
int mid = (end - start) / 2 + start;
String lcpLeft = longestCommonPrefix(strs, start, mid);
String lcpRight = longestCommonPrefix(strs, mid + 1, end);
return commonPrefix(lcpLeft, lcpRight);
}
}
public String commonPrefix(String lcpLeft, String lcpRight) {
int minLength = Math.min(lcpLeft.length(), lcpRight.length());
for (int i = 0; i < minLength; i++) {
if (lcpLeft.charAt(i) != lcpRight.charAt(i)) {
return lcpLeft.substring(0, i);
}
}
return lcpLeft.substring(0, minLength);
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
方法四:二分查找
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int minLength = Integer.MAX_VALUE;
for (String str : strs) {
minLength = Math.min(minLength, str.length());
}
int low = 0, high = minLength;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (isCommonPrefix(strs, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return strs[0].substring(0, low);
}
public boolean isCommonPrefix(String[] strs, int length) {
String str0 = strs[0].substring(0, length);
int count = strs.length;
for (int i = 1; i < count; i++) {
String str = strs[i];
for (int j = 0; j < length; j++) {
if (str0.charAt(j) != str.charAt(j)) {
return false;
}
}
}
return true;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/longest-common-prefix/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。