问题描述:
Write a function to find the longest common prefix string amongst an array of strings.
代码如下:328ms
public String longestCommonPrefix(String[] strs) {
int row = strs.length;
if(row<=0)
return "";
int col = strs[0].length();
char tmp;
int i=0,j=0;
for(i = 0;i<col;i++){
tmp = strs[0].charAt(i);
for(j = 1;j<row;j++){
if(strs[j].length()==i||strs[j].charAt(i) != tmp)
break;
}
if(j!=row)
break;
}
return strs[0].substring(0, i);
}