leetcode [#28] | GCidea's blog
目录

题目
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解决方案
1 | public class Solution { |
注意事项
- 实现查询子字符串开始位置下标的方法,在java中即
indexOf(),当然不能直接使用这个方法。 - 将两个字符串均转化成字符数组。
- 对父字符串只需要遍历0~lenHaystack - lenHayNeedle这一段,因为如果包含子字符串,那么开始下标一定位于这一段。
- 内层遍历子字符串,和父字符串当前段逐一比较即可。