strStr
For a given source string and a target string, you should output thefirstindex(from 0) of target string in source string.public int strStr(String source, String target) {
// write your code here
if(source == null || target == null)
return -1;
int sl = source.length();
int tl = target.length();
if(tl == 0)
return 0;
for(int i = 0; i < sl - tl + 1; i ++){
int j = 0;
for(j = 0; j < tl; j ++){
if(source.charAt(i + j) != target.charAt(j)){
break;
}
if(j == tl - 1){
return i;
}
}
}
return -1;
}Last updated