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長度。