赞
踩
题目一:实现strStr()
题目描述:
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1
思路分析:KMP解题,KMP的经典思想就是:当出现字符串不匹配时,可以记录一部分之前已经匹配的文本内容,利用这些信息避免从头再去做匹配。视频指路
- class Solution {
- public int strStr(String haystack, String needle) {
- if (needle.length() == 0) return 0;
-
- int[] next = new int[needle.length()]; // next数组的长度与模式串相同
- getNext(next, needle);
-
- int j = 0;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。