-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode28.java
More file actions
67 lines (60 loc) · 2.06 KB
/
LeetCode28.java
File metadata and controls
67 lines (60 loc) · 2.06 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
public class LeetCode28 {
public static void main(String[] args) {
// 输入:haystack = "sadbutsad", needle = "sad"
// 输出:0
System.out.println(new Solution28().strStr("sadbutsad", "sad"));
// 输入:haystack = "leetcode", needle = "leeto"
// 输出:-1
System.out.println(new Solution28().strStr("leetcode", "leeto"));
// 输入:haystack = "hello", needle = "ll"
// 输出:2
System.out.println(new Solution28().strStr("hello", "ll"));
// 输入:haystack = "mississippi", needle = "issip"
// 输出:4
System.out.println(new Solution28().strStr("mississippi", "issip"));
}
}
class Solution28 {
public int strStr(String haystack, String needle) {
int[] nextArray = getNextArray(needle);
// System.out.println(Arrays.toString(nextArray));
int index = 0;
int j = 0;
while (index < haystack.length()) {
if (haystack.charAt(index) == needle.charAt(j)) {
index++;
j++;
if (j == needle.length()) {
return index - needle.length();
}
} else {
if (j == 0) {
index++;
} else {
j = nextArray[j - 1];
}
}
}
return -1;
}
private int[] getNextArray(String needle) {
int[] nextArray = new int[needle.length()];
for (int i = 1; i < nextArray.length; i++) {
int index = i - 1;
char c = needle.charAt(i);
while (true) {
if (needle.charAt(nextArray[index]) == c) {
nextArray[i] = nextArray[index] + 1;
break;
} else {
index = nextArray[index] - 1;
if (index <= 0) {
nextArray[i] = c == needle.charAt(0) ? 1 : 0;
break;
}
}
}
}
return nextArray;
}
}