I have a problem to asking you

  • 0
    int strStr(string haystack, string needle){
    if (!needle[0]) return 0;
    int L = haystack.length()-needle.length()+1;
    for (int i = 0;i<L;i++){ //为什么这个语句不对,换成L就对了
    	if (haystack[i]==needle[0]){
    		int j = 0;
    		while(++j<needle.length() && haystack[++i]==needle[j]);
    		  if (j == needle.length() )   return i-needle.length()+1;
    		  else i -= j;			
      return -1;
      // Now,if we replace     "for (int i = 0;i<L;i++){"    with
      // "for (int i = 0;i<haystack.length()-needle.length()+1;i++){"   
      // the code can't be accepted for the case that haystack, "abb" ,
     // and needle,"abaaa".I dot know why, is there anybody know the reason?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.