原题:(频率5)
mplement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题意:求子串needle在haystack中的位置
代码和思路:
class Solution {
public int strStr(String haystack, String needle) {
int l1 = haystack.length();
int l2 = needle.length();
if(l1<l2) return -1;
else if(l2==0) return 0;
//l1比l2长多少
int exceed = l1 - l2;
for(int i=0;i<=exceed;i++){
if(haystack.substring(i,i+l2).equals(needle)){
return i;
}
}
return -1;
}
}