ninehills / blog

https://ninehills.tech
862 stars 80 forks source link

LeetCode-28. Implement strStr() #44

Closed ninehills closed 6 years ago

ninehills commented 7 years ago

问题

https://leetcode.com/problems/implement-strstr/description/ Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路

字符串子串搜索,两种思路:

  1. 暴力查找,时间复杂度O(N*M),原理比较简单的,就是循环比对
  2. KMP算法

解答

ninehills commented 7 years ago

4