问题描述:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
下面附代码:
public class Solution {
public int strStr(String haystack, String needle) {
if(needle.equals("") && haystack.equals("")) return 0;
int hlength = haystack.length();
int nlength = needle.length();
for(int i=0;i<=hlength-nlength;i++){
String s1 = haystack.substring(i,i+nlength);
if(s1.equals(needle))
return i;
}
return -1;
}
}