28. Implement strStr()
- 注意各种边界的判断
if (haystack == null ||needle == null) {
return -1;
}
for(int i=0;i<haystack.length()-needle.length()+1;i++){
int j=0;
for(;j<needle.length();j++){
if(haystack.charAt(i+j) != needle.charAt(j)) break;
}
if(j == needle.length()) return i;
}
return -1;

浙公网安备 33010602011771号