Leetcode 28. Implement StrStr ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.