-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementstrStr.java
More file actions
43 lines (38 loc) · 893 Bytes
/
ImplementstrStr.java
File metadata and controls
43 lines (38 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.mirraico.leetcode;
public class ImplementstrStr {
public int strStr(String haystack, String needle) {
int hLen = haystack.length(), nLen = needle.length();
if(nLen == 0) return 0;
int[] next = new int[nLen];
next[0] = -1;
int j = 0, k = -1;
while(j < nLen - 1) {
if(k == -1 || needle.charAt(j) == needle.charAt(k)) {
j++; k++;
next[j] = k;
/* 优化
if(needle.charAt(j) != needle.charAt(k)) {
next[j] = k;
} else {
next[j] = next[k];
}
*/
} else {
k = next[k];
}
}
int i = 0; j = 0;
while(i < hLen && j < nLen) {
if(j == -1 || haystack.charAt(i) == needle.charAt(j)) {
i++; j++;
} else {
j = next[j];
}
}
if(j == nLen) return i - j;
else return -1;
}
public static void main(String[] args) {
System.out.println(new Solution().strStr("BBABCDABCABCDABCDABDE", "ABCDABD"));
}
}