编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""
。
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0)
return "";
String res=strs[0];
int i=1;
while(i<strs.length){
while(strs[i].indexOf(res)!=0){
res=res.substring(0,res.length()-1);
}
i+=1;
}
return res;
}
}