Skip to content

Commit 8a4378d

Browse files
committed
update: 028
1 parent 0f8c08c commit 8a4378d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

note/028/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ Tags:** Two Pointers, String
2525

2626
## 思路
2727

28-
题意是从主串中找到子串的索引,如果找不到则返回-1,当字串长度大于主串,直接返回-1,然后我们只需要遍历比较即可。
28+
题意是从主串中找到子串的索引,如果找不到则返回-1,当子串长度大于主串,直接返回-1,然后我们只需要遍历比较即可。
2929

3030
```java
3131
class Solution {
3232
public int strStr(String haystack, String needle) {
3333
int l1 = haystack.length(), l2 = needle.length();
3434
if (l1 < l2) return -1;
3535
for (int i = 0; ; i++) {
36+
if (i + l2 > l1) return -1;
3637
for (int j = 0; ; j++) {
3738
if (j == l2) return i;
38-
if (i + j == l1) return -1;
3939
if (haystack.charAt(i + j) != needle.charAt(j)) break;
4040
}
4141
}

src/com/blankj/easy/_028/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public int strStr(String haystack, String needle) {
1313
int l1 = haystack.length(), l2 = needle.length();
1414
if (l1 < l2) return -1;
1515
for (int i = 0; ; i++) {
16+
if (i + l2 > l1) return -1;
1617
for (int j = 0; ; j++) {
1718
if (j == l2) return i;
18-
if (i + j == l1) return -1;
1919
if (haystack.charAt(i + j) != needle.charAt(j)) break;
2020
}
2121
}

0 commit comments

Comments
 (0)