一份是C++代码:

  [cpp] view plaincopyprint?

  //copyright@caopengcs 2013/10月

  class Solution {

  public:

  char *strStr(char *haystack, char *needle) {

  // Start typing your C/C++ solution below

  // DO NOT write int main() function

  int i,j;

  for (i = j = 0; haystack[i] && needle[j];) {

  if (haystack[i] == needle[j]) {

  ++i;

  ++j;

  }

  else {

  i = i - j + 1;

  j = 0;

  }

  }

  return needle[j]?0:(haystack + i - j);

  }

  };

  //copyright@caopengcs 2013/10月

  class Solution {

  public:

  char *strStr(char *haystack, char *needle) {

  // Start typing your C/C++ solution below

  // DO NOT write int main() function

  int i,j;

  for (i = j = 0; haystack[i] && needle[j];) {

  if (haystack[i] == needle[j]) {

  ++i;

  ++j;

  }

  else {

  i = i - j + 1;

  j = 0;

  }

  }

  return needle[j]?0:(haystack + i - j);

  }

  };