我的思路是暴力枚举:
情况1:相同,就让子串和原串同时后移继续比较情况2:不相同,就只让原串后移
public int strStr(String haystack, String needle) {if (haystack.length() < needle.length()){return -1;}for (int i = 0; i <= haystack.length() - needle.length(); i++) {int j = 0;//要使: i + j 不越界 因为j!=needle.length() 所以i 可以 = haystack.length() - needle.length()while (j < needle.length() && haystack.charAt(i + j) == needle.charAt(j)){j++;}if (j == needle.length()){//最后一个元素也判断完return i;}}return -1;}