Write a function to find the longest common prefix string amongst an array of strings.
----------------------------------------------------------------------------------------------------
这道题没有什么特别要注意的,
就是将第一个string 的i 位上的char 与 剩下的 string的 i 位上的char进行比较。
代码:
public class LongestCommonPrefix {
public String longestCommonPrefix(String[] strs) {
int index = 0;
if (strs.length == 0) {
return "";
}
Arrays.sort(strs);
String first = strs[0];
while (index < strs[0].length()) {
for (int i = 1; i < strs.length; i++) {
if (index >= strs[i].length() || first.charAt(index) != strs[i].charAt(index)) {
return first.substring(0, index);
}
}
index++;
}
return first;
}
}