题目链接:https://leetcode.com/problems/longest-common-prefix/
题目:
Write a function to find the longest common prefix string amongst an array of strings.
算法:
-
public String longestCommonPrefix(String[] strs) {
-
if (strs.length == 0) {
-
return "";
-
}
-
int miniLength = strs[0].length();
-
for (String s : strs) {
-
if (s.length() < miniLength) {
-
miniLength = s.length();
-
}
-
}
-
for (int j = 0; j < miniLength; j++) {
-
for (int i = 0; i < strs.length; i++) {
-
if (strs[0].charAt(j) != strs[i].charAt(j)) {
-
return strs[0].substring(0, j);
-
}
-
}
-
}
-
return strs[0].substring(0, miniLength);
-
}
【Leetcode】Longest Common Prefix
原文:http://blog.csdn.net/yeqiuzs/article/details/51590935