Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String , please click on the Reload button to reset your Code definition.
This problem is implemented in the haystack string to find the first occurrence of the needle index number. Using brute force search is relatively easy to achieve.
Class Solution {public: int strStr (String haystack, string needle) { int length_h = Haystack.length (); int length_n = Needle.length (); int step = length_h-length_n + 1; for (int i = 0; i < step; i++) { int J; for (j = 0; J < Length_n; J + +) { if (haystack[i+j] = = Needle[j]) continue; else break ; } if (j = = Length_n) return i; } return-1; }};
Leetcode37:implement StrStr ()