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