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

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

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