1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-20 22:17:09 +08:00

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

原文中的校验,存在边界问题,当 String[] strs = {};或者 String[] strs = null;时仍然会报错。
This commit is contained in:
【张鑫】 2019-07-25 10:17:15 +08:00 committed by GitHub
parent ac3a10a1ff
commit 47776dbf46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,17 +135,20 @@ public class Main {
} }
private static boolean checkStrs(String[] strs) { private static boolean chechStrs(String[] strs) {
if (strs != null) { boolean flag = false;
// 遍历strs检查元素值 if (strs != null) {
for (int i = 0; i < strs.length; i++) { // 遍历strs检查元素值
if (strs[i] == null || strs[i].length() == 0) { for (int i = 0; i < strs.length; i++) {
return false; if (strs[i] != null && strs[i].length() != 0) {
} flag = true;
} } else {
} flag = false;
return true; }
} }
}
return flag;
}
// 测试 // 测试
public static void main(String[] args) { public static void main(String[] args) {