Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
翻译:实现strStr()函数
返回needle在haystack中的首个索引位置
思路:最懵逼的用indexOf函数解决了问题,感觉不对啊,答案也有比较原始的解法,但是我觉得有这么好用的函数为什么不用呢,又不用考虑性能。
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
原始答案思路是用循环解决依次判断每一位是否相同,根据同时看是否达到needle长度或超出haystack长度。