/*
* 注意题中两个字符串有一个为空时返回的是“”而不是null
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {if(strs==null||strs.length==0) return "";
int nums = strs.length;
String commonPreix = strs[0];
for(int i=1;i<nums;++i){
commonPreix = findPrefix(commonPreix, strs[i]);
}
return commonPreix;
}
public String findPrefix(String s1, String s2){
if(s1==null||s2==null) return "";
StringBuffer buffer = new StringBuffer();
int s1_len = s1.length();
int s2_len = s2.length();
int p = 0, q = 0;
while(p<s1_len && q<s2_len){
if(s1.charAt(p)==s2.charAt(q)){
buffer.append(s1.charAt(p));
++p;
++q;
}else{
break;
}
}
return buffer.toString();
}
}