题目:
Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question.
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0)
return "";
String result = strs[0];
int i=1;
while(i<strs.length){
while(!strs[i].startsWith(result)){
result = result.substring(0, result.length()-1);
}
i++;
}
return result;
}
}