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;

posted @ 2017-01-14 05:21  EnoWang  阅读(35)  评论(0)    收藏  举报