Implement StrStr ()

Source: Internet
Author: User

    /** 28.      Implement strStr (); * 2016-4-16 by Mingyang * Very simple thinking, just beginning to write their own messy, back to learn from the online concise code * using SUBSTRING to express the process of intercepting a string, very effective, * so learn from the subst Ring and other self-band functions*/      Public intstrStr1 (String haystack, string needle) {intlen1=haystack.length (); intLen2=needle.length (); if(len1==0){                returnLen2==0?0:-1; }            if(len2==0)                return0; if(len1<len2) {                return-1; }            //the len1-len2 here need to find a good             for(intI=0;i<= (LEN1-LEN2); i++){                intJ=0; intk=i;  while(j<len2&&k<len1) {                    if(Haystack.charat (k) = =Needle.charat (j)) {k++; J++; }Else{                         Break; }                }                if(j==len2) {                    returnI//It's a little easier here .                }            }            return-1; }     Public intstrStr (String haystack, string needle) {intL1 = Haystack.length (), L2 =needle.length (); if(L1 <L2) {            return-1; } Else if(L2 = = 0) {            return0; }        intthreshold = L1-L2;  for(inti = 0; I <= threshold; ++i) {if(Haystack.substring (i, i +L2). Equals (needle)) {returni; }        }        return-1; }

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.