-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStrstr.java
More file actions
67 lines (62 loc) · 2.11 KB
/
ImplementStrstr.java
File metadata and controls
67 lines (62 loc) · 2.11 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package leetcode;
/**
* ImplementStrstr
* https://leetcode-cn.com/problems/implement-strstr/
* 28. 实现 strStr()
* https://leetcode-cn.com/problems/implement-strstr/solution/kmpxue-xi-zhang-wo-by-oshdyr-0m85/
*
* @author tobin
* @since 2021-04-20
*/
public class ImplementStrstr {
public static void main(String[] args) {
ImplementStrstr sol = new ImplementStrstr();
System.out.println(sol.strStr("hello", "ll"));
System.out.println(sol.strStr("aaaaaa", "bba"));
System.out.println(sol.strStr("", ""));
System.out.println(sol.strStr("aaaaacd", "aac")); // 看它的前一个字符的前缀表的数值是多少
System.out.println(sol.strStr("aa", "aa"));
System.out.println(sol.strStr("bbbbababbbaabbba", "abb"));
System.out.println(sol.strStr("adcadcaddcadde", "adcadde"));
}
public int strStr(String haystack, String needle) {
if (needle == null || needle.isEmpty()) {
return 0;
}
int[] next = new int[needle.length()]; // next数组, KEY 1
next[0] = 0;
int j = 0;
for (int i = 1; i < needle.length(); i++) {
while (j >= 0) {
if (needle.charAt(j) == needle.charAt(i)) {
break;
}
if (j == 0) {
j = j - 1;
break;
}
j = next[j - 1]; // 同样是根据前缀来找, KEY 4
}
j++;
next[i] = j; // j + 1后再赋值, KEY 2
}
int moveJ = 0;
for (int i = 0; i < haystack.length(); i++) {
while (moveJ >= 0) {
if (haystack.charAt(i) == needle.charAt(moveJ)) {
moveJ++;
break;
}
if (moveJ == 0) {
break;
}
// 看它的前一个字符的前缀表的数值是多少, KEY 3
moveJ = next[moveJ - 1];
}
if (moveJ == needle.length()) {
return i + 1 - moveJ;
}
}
return -1;
}
}