forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrStr.java
More file actions
19 lines (18 loc) · 568 Bytes
/
StrStr.java
File metadata and controls
19 lines (18 loc) · 568 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class StrStr {
// 这里非常重要的是i<=len1-len2,如果没有这个会超时
// 比如needle非常长的时候
public int strStr(String haystack, String needle) {
int l1 = haystack.length(), l2 = needle.length();
for (int i = 0, j; i + l2 - 1 < l1; i++) {
for (j = 0; j < l2; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
}
}
if (j >= l2) {
return i;
}
}
return -1;
}
}