Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
My code:
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
public int strStr(String haystack, String needle) {
for(int i=0; ;i++){
for(int j=0; ;j++){
if(j==needle.length()) return i;
if(i+j==haystack.length()) return -1;
if(needle.charAt(j)!=haystack.charAt(i+j)) break;
}
}
}
总结:遍历,这道题必须要挨个遍历。mississippi","issip”