需要注意strs为空的情况
class Solution {
private int getSumString(String a,String b){
int i=0;
while(i<a.length()&&i<b.length()){
if(a.charAt(i)!=b.charAt(i))break;
i++;
}
return i;
}
public String longestCommonPrefix(String[] strs) {
if(strs.length==0)return "";
int publicLength = strs[0].length();
for(int i=0;i<strs.length;i++){
int sumStr = getSumString(strs[0],strs[i]);
if(sumStr<publicLength)publicLength=sumStr;
}
if(publicLength<=0)return "";
else return strs[0].substring(0,publicLength);
}
}
网友评论