Implement StrStr ()
Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
find the position that matches the first needle in the haystack. If not found, returns-1.
The code is as follows:
Class solution {public: int strstr (string haystack, string needle) { if (haystack.size () == 0 && needle.size () == 0) return 0; if (Needle.size () == 0) return 0; if (Haystack.size () < needle.size ()) return -1; for (int i = 0;i < haystack.size () - needle.size () + 1;i++) { bool flag = true; if (Needle[0] == haystack[i]) { int j = 0; for (; j < Needle.size (); j + +) { if (Needle[j] != haystack[i+j]) { Flag = false; break; } } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (flag) return i; } } return -1; }};
2016-08-11 01:02:49
This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1836727
Leetcode 28. Implement strStr () string