给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
滑动窗口法比较容易理解,kmp算法待完善
public static int strStr(String haystack, String needle) {
//首先第一种是滑动窗口解法
int t=haystack.length(),b=needle.length();
for (int i = 0; i <t-b+1 ; i++) {
if(haystack.substring(i,i+b).equals(needle)){
return i;
}
}
return -1;
}