Write a function to find the longest common prefix string amongst an array of strings.
这道题采用brute_force的方法,以第一个字符串为标准,后面的字符串与这个字符串进行匹配
代码如下
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
int index = 0;
StringBuilder result = new StringBuilder();
while (index < strs[0].length()) {
for (int i = 0; i < strs.length; i++) {
if (index > strs[i].length() - 1
|| strs[i].charAt(index) != strs[0].charAt(index))
return result.toString();
}
result.append(strs[0].charAt(index++));
}
return result.toString();
}