題目
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
給定兩個字串needle和,傳回在中haystack第一次出現的索引,或如果不是的一部分。needlehaystack-1needlehaystack
解題思路
從母字串的每一個起始位置 i 嘗試比對 needle。
若全部匹配成功 → 回傳 i。
若比對到尾端都沒找到 → 回傳 -1。
時間複雜度:O(n*m)。
解題思路
從母字串的每一個起始位置 i 嘗試比對 needle。
若全部匹配成功 → 回傳 i。
若比對到尾端都沒找到 → 回傳 -1。
時間複雜度:O(n*m)。