package javaLeetCode.primary;
/**
* Write a function to find the longest common prefix string amongst an array of strings.
* If there is no common prefix, return an empty string "".
* */publicclassLongestCommonPrefix_14 {
publicstaticvoidmain(String[] args) {
String[] strs = newString[]{"flower","flow","flight"};
System.out.println(longestCommonPrefix_1(strs));
}//end main()/**
* Make full use of the methods of the String class.<br>
* 1. Find the shortest string.<br>
* 2. Compare it to each element of the string array from beginning to end.<br>
* 3. If it does not match any of the elements in the string array, it is clipped off the last character and out of the for loop.<br>
* 4. When its length is zero, break out of the while loop.
* *//*
* Test Data:
* ["dog","racecar","car"]
* ["flower","flow","flight"]
* ["ca","a"]
* ["a"]
* ["aca","cba"]
* */publicstatic String longestCommonPrefix_1(String[] strs) {
if (strs == null || strs.length == 0)
return"";
// Find the shortest string.StringminString= strs[0];
for(int i=1;i<strs.length;i++) {
if(minString.length()>strs[i].length()) {
minString = strs[i];
}else {
continue;
}//end if
}//end for//Find whether other strings include the minString.booleanisInclude=true;
while(minString!=null) {
for(int i=0;i<strs.length;i++) {
if(strs[i].startsWith(minString)) {
isInclude = true;
}else {
minString=minString.substring(0, minString.length()-1);
isInclude = false;
break;
}//end if
}//end forif(isInclude == true)
break;
else ;
}//end whilereturn minString;
}//end longestCommonPrefix()/**
* Use the simply method.
* 1. Find the shortest string.<br>
* 2. Using two for loops, the outer loop's control condition is the length of the shortest string,
* and the memory loop's control condition is each element of the string array<br>
* */publicstatic String longestCommonPrefix_2(String[] strs) {
if (strs == null || strs.length == 0)
return"";
// Find the shortest string.StringminString= strs[0];
for(int i=1;i<strs.length;i++) {
if(minString.length()>strs[i].length()) {
minString = strs[i];
}else {
continue;
}//end if
}//end for//Find whether other strings include the minString.booleanisInclude=true;
StringBuilderstr=newStringBuilder();
for (intj=0; j < minString.length(); j++) {
for (inti=0; i < strs.length-1; i++) {
if (strs[i].charAt(j) == strs[i + 1].charAt(j)) {
isInclude = true;
} else {
isInclude = false;
break;
}//end if
}// end forif(isInclude==true){
str.append(minString.charAt(j));
}elseif(isInclude == false&&j==0) {
break;
}//end if
}// end forreturn String.valueOf(str);
}//end longestCommonPrefix()/**
* Answer online.
* Horizontal scanning
* */public String longestCommonPrefix_3(String[] strs) {
if (strs.length == 0) return"";
Stringprefix= strs[0];
for (inti=1; i < strs.length; i++)
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return"";
}
return prefix;
}//end longestCommonPrefix()/**
* Answer online.
* Binary search
* */publicstatic String longestCommonPrefix_4(String[] strs) {
if (strs == null || strs.length == 0)
return"";
intminLen= Integer.MAX_VALUE;
for (String str : strs)
minLen = Math.min(minLen, str.length());
intlow=1;
inthigh= minLen;
while (low <= high) {
intmiddle= (low + high) / 2;
if (isCommonPrefix(strs, middle))
low = middle + 1;
elsehigh= middle - 1;
}
return strs[0].substring(0, (low + high) / 2);
}
/**aiding method */privatestaticbooleanisCommonPrefix(String[] strs, int len){
Stringstr1= strs[0].substring(0,len);
for (inti=1; i < strs.length; i++)
if (!strs[i].startsWith(str1))
returnfalse;
returntrue;
}
}//end LongestCommonPrefix_14
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步