未验证 提交 47776dbf 编写于 作者: 【张鑫】 提交者: GitHub

最长公共前缀题目数组检测函数修复

原文中的校验,存在边界问题,当 String[] strs = {};或者 String[] strs = null;时仍然会报错。
上级 ac3a10a1
......@@ -135,17 +135,20 @@ public class Main {
}
private static boolean checkStrs(String[] strs) {
if (strs != null) {
// 遍历strs检查元素值
for (int i = 0; i < strs.length; i++) {
if (strs[i] == null || strs[i].length() == 0) {
return false;
}
}
}
return true;
}
private static boolean chechStrs(String[] strs) {
boolean flag = false;
if (strs != null) {
// 遍历strs检查元素值
for (int i = 0; i < strs.length; i++) {
if (strs[i] != null && strs[i].length() != 0) {
flag = true;
} else {
flag = false;
}
}
}
return flag;
}
// 测试
public static void main(String[] args) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册