Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
原题链接:https://oj.leetcode.com/problems/implement-strstr/题目:实现strStr()函数。返回子串在母串中到开始出现的位置。
最直接的暴力查找。
public static String strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return null;
}
int i, j;
for (i = 0; i < haystack.length() - needle.length() + 1; i++) {
for (j = 0; j < needle.length(); j++) {
if (haystack.charAt(i + j) != needle.charAt(j))
break;
}
if (j == needle.length())
return haystack.substring(i);
}
return null;
}