【题目】Write a function to find the longest common prefix string amongst an array of strings.
【代码】
public class Solution {
public static String longestCommonPrefix(String[] strs) {
int temp = 0;
String result = "";
ok: for (int i = 0; i < strs[0].length();)
for (int j = 1; j < strs.length; j++) {
if (i < strs[j].length() && strs[0].charAt(i) == strs[j].charAt(i)) {
temp++;
}
if (temp != j) {
break ok;
}
if (temp == strs.length - 1) {
result = result + strs[0].charAt(i);
temp = 0;
i++;
}
}
return result;
}
}
【注意】加入识别字跳出多重循环