Skip to content

Commit 47776db

Browse files
authored
最长公共前缀题目数组检测函数修复
原文中的校验,存在边界问题,当 String[] strs = {};或者 String[] strs = null;时仍然会报错。
1 parent ac3a10a commit 47776db

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

docs/dataStructures-algorithms/几道常见的子符串算法题.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,20 @@ public class Main {
135135

136136
}
137137

138-
private static boolean checkStrs(String[] strs) {
139-
if (strs != null) {
140-
// 遍历strs检查元素值
141-
for (int i = 0; i < strs.length; i++) {
142-
if (strs[i] == null || strs[i].length() == 0) {
143-
return false;
144-
}
145-
}
146-
}
147-
return true;
148-
}
138+
private static boolean chechStrs(String[] strs) {
139+
boolean flag = false;
140+
if (strs != null) {
141+
// 遍历strs检查元素值
142+
for (int i = 0; i < strs.length; i++) {
143+
if (strs[i] != null && strs[i].length() != 0) {
144+
flag = true;
145+
} else {
146+
flag = false;
147+
}
148+
}
149+
}
150+
return flag;
151+
}
149152

150153
// 测试
151154
public static void main(String[] args) {

0 commit comments

Comments
 (0)