【题目】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;
}
}【注意】加入识别字跳出多重循环
本博客介绍了一个函数,用于找出给定字符串数组中最长的公共前缀字符串。通过遍历数组并比较字符,实现高效查找。适用于字符串处理和算法应用。
113

被折叠的 条评论
为什么被折叠?



