Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8a4378d

Browse filesBrowse files
committed
update: 028
1 parent 0f8c08c commit 8a4378d
Copy full SHA for 8a4378d

File tree

Expand file treeCollapse file tree

2 files changed

+3
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+3
-3
lines changed

‎note/028/README.md

Copy file name to clipboardExpand all lines: note/028/README.md
+2-2Lines changed: 2 additions & 2 deletions
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

Copy file name to clipboardExpand all lines: src/com/blankj/easy/_028/Solution.java
+1-1Lines changed: 1 addition & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.