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 }
是int index = haystack.indexOf(needle)的另一种做法
时间: 2024-10-12 12:30:59