Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
分析:
从0开始每次截取needle长度的字串来匹配,如果相等则找到。
public class Solution {
public String strStr(String haystack, String needle) {
int len = needle.length();
for(int i=0; i<=haystack.length()-len; i++){
String sub = haystack.substring(i,i+len);
if(sub.equals(needle)){
return haystack.substring(i);
}
}
return null;
}
}