題目:
這道題力扣和牛客的題目略微不同,所以我以力扣爲準
解題思路:
滑動窗口
https://leetcode-cn.com/problems/implement-strstr/solution/shi-xian-strstr-by-leetcode/
代碼:
public class LC122 {
public int strStr(String haystack, String needle) {
int L = needle.length(), n = haystack.length();
for (int start = 0; start < n - L + 1; ++start) {
if (haystack.substring(start, start + L).equals(needle)) {
return start;
}
}
return -1;
}
public static void main(String[] args) {
LC122 lc122 = new LC122();
System.out.println(lc122.strStr("noddle", "ddle"));
}
}