首页 > 代码库 > 刷题之Implement strStr()

刷题之Implement strStr()

技术分享
 1 public class Solution {
 2     public int strStr(String haystack, String needle) {
 3         int big = haystack.length();
 4         int sub = needle.length();
 5         if(big==0 && sub==0) return 0;
 6         if(big==0 && sub!=0) return -1;
 7         int index = big-sub;
 8         for(int i=0;i<=index;i++){
 9             if(haystack.substring(i,sub+i).equals(needle))return i;
10         }
11         return -1;
12     }
13 }
View Code

 是int index = haystack.indexOf(needle)的另一种做法

刷题之Implement strStr()