Leetcode Problem.28—Implement strStr()

来源:互联网 发布:淘宝几毛钱的东西 编辑:程序博客网 时间:2024/06/06 01:03

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.


My C++ solution!

    int strStr(string haystack, string needle)    {      int len1=haystack.length();  int len2=needle.length();  if(len1<len2)  return -1;   int j;  for(int i=0;i<=len1-len2;i++)  { for(j=0;j<len2;j++) { if(haystack[i+j]==needle[j]) continue; else break; } if(j==len2) return i;  }   return -1;            }


0 0
原创粉丝点击