Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解:注意发生不匹配时重写匹配的起始位置。
public int strStr(String haystack, String needle) {
int i = 0,j = 0;
if (needle == null) return -1;
if (needle.length() == 0) return 0;
while(i<haystack.length()){
while (j<needle.length()){
if (haystack.charAt(i) == needle.charAt(j)){
j++;
i++;
break;
}else {
//注意不满足条件时 i返回到原来开始匹配位置的下一位
i=i+1-j;
j=0;
break;
}
}
if (j==needle.length()) return i-needle.length();
}
return -1;
}