Implement strStr()

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {
 4         if(haystack.empty()&&!needle.empty()) return -1;
 5         int i=0,j=0;
 6         while(i<haystack.size()&&j<needle.size())
 7         if(haystack[i]==needle[j])
 8          {i++;j++;}
 9         else {i=i-j+1;j=0;}
10         if(j>=needle.size())return i-j;
11         else return -1;
12     }
13 };
原文地址:https://www.cnblogs.com/daocaorenblog/p/4975408.html