https://leetcode.com/problems/implement-strstr/
Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
- String simple question. Be sure to write no bugs.
- If the return statement is not written in the last line of the function, Leetcode will have runtime ERROR.
Line 27:control reaches end of non-void function [-werror=return-type]
- Strstr-c++ Reference
- Http://www.cplusplus.com/reference/cstring/strstr/?kw=strstr
1#include <iostream>2#include <string>3 using namespacestd;4 5 classSolution {6 Public:7 intSTRSTR (stringHaystackstringneedle)8 {9 //Corner CaseTen if(Needle.empty ())return 0; One if(Haystack.empty ())return-1; A - intn = haystack.length ()-needle.length () +1; - the //better use < rather than <= - for(inti =0; I < n; i + +) - { - BOOLFlag =true; + - for(intj =0; J < Needle.length (); J + +) + { A if(Haystack.at (i + j)! =needle.at (j)) at { -Flag =false; - Break; - } - } - in if(flag) - returni; to } + - return-1; the } * }; $ Panax Notoginseng intMain () - { the solution testsolution; + stringS1 ="Mississippi"; A stringS2 ="ISSI"; the +cout << testsolution.strstr (S1, S2) <<Endl; - $ GetChar (); $ - return 0; -}
View Code
Leetcode 28. Implement StrStr ()