时间复杂度:
public int strStr(String haystack, String needle) {int[] next = new int[needle.length()];//next数组的生成next[0] = 0;int prefixLen = 0;//共同前后缀长度int i = 1, j = 1;//i,j复用while (i < needle.length()) {if (needle.charAt(prefixLen) == needle.charAt(i)) {prefixLen++;next[j++] = prefixLen;i++;} else {if (prefixLen == 0) {next[j++] = 0;i++;} else {prefixLen = next[prefixLen - 1];}}}i = j = 0;while (i < haystack.length()) {if (haystack.charAt(i) == needle.charAt(j)) {i++;j++;} else if (j > 0) {j = next[j - 1];} else {i++;}if (j == needle.length())return i - j;}return -1;}