题目链接:https://leetcode-cn.com/problems/longest-common-prefix/
题目如下:
class Solution {
public String longestCommonPrefix(String[] strs) {
int count=strs.length;
if(count==0) return "";//缺少这一条会报越界错,可能存在空数组
int minlen=strs[0].length();
int pos=0;
String substr="";
for(int i=1;i<count;i++)//找出长度最小的字符串
if(minlen>strs[i].length()) {minlen=strs[i].length();pos=i;}
for(int i=1;i<=minlen;i++)//对长度最小的字符串依次取它的子串
{
substr=strs[pos].substring(0,i);
for(int j=0;j<strs.length;j++)
if(substr.equals(strs[j].substring(0,i))==false) return substr.substring(0,i-1);
}
return substr;
}
}