Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question
哎。。不认识prefix。。。脑残啊
public class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = "";
if(strs.length == 0)
return prefix;
if(strs.length == 1)
return strs[0];
int minlength = strs[0].length();
for(int i = 0; i < strs.length; i++){
if (minlength > strs[i].length())
minlength = strs[i].length();
}
for(int index = 0; index < minlength; index++){
char ch = strs[0].charAt(index);
for(String str : strs){
if (ch!= str.charAt(index))
return prefix;
}
prefix += ch;
}
return prefix;
}
}