题目要求:
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题目不难,可以直接使用暴力搜索的方式进行处理
public class solution {
public int strStr(String haystack, String needle) {
//使用暴力搜索的方式进行求解
int l1 = haystack.length();
int l2 = needle.length();
if(l1 == l2 && l1 == 0) return 0;
int j = 0;
for(int i = 0; i <= l1 - l2; i++) {
for(j = 0; j < l2; j++) {
if(haystack.charAt(i + j) != needle.charAt(j)) break;
}
if(j == l2) return i;
}
return -1;
}
}