LeetCode 28. Implement strStr() - KMP

36 篇文章 0 订阅
30 篇文章 1 订阅

题目链接:28. Implement strStr()

Implement strStr().

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

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

题解

双指针算法就是从匹配串的第一个字符开始一个一个的匹配,直到结束或找到为止。

KMP算法只写的出来代码,next数组的构造还理不清楚。

第一个KMP算法的题目,方便以后查找。

Java代码

暴力搜索算法(双指针)
class Solution {
	public int strStr(String source, String target) {
		if (target == null || target.equals("")) {
			return 0;
		}
		if (source == null || source.equals("")) {
			return -1;
		}

		char[] sourceArr = source.toCharArray();
		char[] targetArr = target.toCharArray();
		int sourceCount = sourceArr.length;
		int targetCount = targetArr.length;

		if (sourceCount < targetCount) {
			return -1;
		}

		int i = 0;
		int j = 0;
		while (i < sourceCount && j < targetCount) {
			if (sourceArr[i] == targetArr[j]) {
				++i;
				++j;
			} else {
				i = i - j + 1;
				j = 0;
			}
			if (j == targetCount) {
				return i - j;
			}
		}
		return -1;
	}
}
KMP算法
/**
 * 2020-2-4 16:31:19
 */
class Solution {
    public int strStr(String source, String target) {
        if (target == null || target.equals("")) {
            return 0;
        }
        if (source == null || source.equals("")) {
            return -1;
        }

        char[] sourceArr = source.toCharArray();
        char[] targetArr = target.toCharArray();

        if (sourceArr.length < targetArr.length) {
            // 源字符串长度小于目标字符串长度,肯定找不到
            return -1;
        }

        // 构造二维数组next,next[i][j]=k表示已经匹配到i个字符,遇到字符j的时候,
        // 下一步的状态是k
        int[][] next = new int[targetArr.length][128];
        // 匹配到0个字符并且遇到目标字符串的第0个字符时,状态改为1
        next[0][targetArr[0]] = 1;
        int index = 0;
        // for循环遍历目标字符串,构造next数组
        for (int i = 1; i < targetArr.length; ++i) {
            // for循环遍历遇见的字符的128种情况
            for (int j = 0; j < 128; ++j) {
                // 检查相同前缀的状态
                next[i][j] = next[index][j];
            }
            // 第i个字符匹配,状态加1
            next[i][targetArr[i]] = i + 1;
            // 更新最大前缀
            index = next[index][targetArr[i]];
        }

        // 查找
        index = 0;
        for (int i = 0; i < sourceArr.length; ++i) {
            index = next[index][sourceArr[i]];
            if (index == targetArr.length) {
                return i - (index - 1);
            }
        }
        return -1;// 未找到目标字符串
    }
}

原文链接:https://blog.csdn.net/pfdvnah/article/details/104171312

- End - wowpH - pfdvnah -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值