"Leetcode" 28. Implement StrStr ()

Source: Internet
Author: User

Title:

Implement strStr ().

Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.

Tips:

This topic can use brute force search methods. Of course, there are other advanced methods such as Rabin-karp, KMP, and so on. These algorithms plan to wait for them to understand later, write some articles to summarize them. So here's just a brute-force search method.

Code:
classSolution { Public:    intSTRSTR (stringHaystackstringneedle) {    intHay_len =haystack.size (); intNeedle_len =needle.size (); if(Needle_len = =0)return 0; if(Hay_len = =0)return-1; inti =0, j =0, p;  for(i =0; I <= Hay_len-needle_len; ++i) { for(P = i, j =0; J < Needle_len; ++j, + +p) {if(haystack[p]! = Needle[j]) Break; }        if(j = = Needle_len)returni; }    return-1;}};

"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.