-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution028.java
71 lines (63 loc) · 1.59 KB
/
Solution028.java
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
68
69
70
71
package algorithm.leetcode;
/**
* @author mayuan
* @desc 实现strStr()
* @date 2018/02/09
*/
public class Solution028 {
/**
* 采用KMP算法实现匹配
* @param haystack
* @param needle
* @return
*/
public int strStr(String haystack, String needle) {
if (null == haystack || null == needle) {
return -1;
}
if (0 == needle.length()) {
return 0;
}
// 主串的位置
int i = 0;
// 模式串的位置
int j = 0;
int[] next = getNext(needle);
while (i < haystack.length() && j < needle.length()) {
if (-1 == j || haystack.charAt(i) == needle.charAt(j)) {
++i;
++j;
} else {
// i不需要回溯,j回到指定位置.
j = next[j];
}
}
// j是模式串指针
// 此时代表模式串完全匹配,则匹配的起始索引为i-j.
if (needle.length() == j) {
return i - j;
} else {
return -1;
}
}
/**
* kmp算法->获得next数组
*
* @param pattern 模式串字符
* @return
*/
public static int[] getNext(String pattern) {
int[] next = new int[pattern.length()];
next[0] = -1;
int k = -1;
int j = 0;
while (j < pattern.length() - 1) {
if (-1 == k || pattern.charAt(j) == pattern.charAt(k)) {
next[++j] = ++k;
} else {
k = next[k];
}
}
return next;
}
}