File tree Expand file tree Collapse file tree 2 files changed +3
-3
lines changed Expand file tree Collapse file tree 2 files changed +3
-3
lines changed Original file line number Diff line number Diff line change @@ -25,17 +25,17 @@ Tags:** Two Pointers, String
2525
2626## 思路
2727
28- 题意是从主串中找到子串的索引,如果找不到则返回-1,当字串长度大于主串 ,直接返回-1,然后我们只需要遍历比较即可。
28+ 题意是从主串中找到子串的索引,如果找不到则返回-1,当子串长度大于主串 ,直接返回-1,然后我们只需要遍历比较即可。
2929
3030``` java
3131class 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 }
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments