Implement strStr()

https://leetcode.com/problems/implement-strstr/

Implement strStr().

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

 1 public class Solution {
 2     public static int strStr(String haystack, String needle) {
 3         if(needle.equals("")){return 0;}
 4         int len1=haystack.length();int len2=needle.length();
 5         for(int i=0;i+len2<=len1;i++){
 6             if(haystack.charAt(i)!=needle.charAt(0)){continue;}
 7             boolean flag=true;
 8             for(int j=1;j<len2;j++){
 9             if(haystack.charAt(i+j)!=needle.charAt(j)){flag=false;break;}
10             }
11             if(flag==true){return i;}
12         }
13         return -1;
14     }
15     public static void main(String[]args){
16     System.out.println(strStr("dddabcfsdf","abc"));
17     }
18 }
原文地址:https://www.cnblogs.com/qq1029579233/p/4480678.html