实现 strStr()

题目描述:实现 strStr() 函数。

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/implement-strstr/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:穷举法
  • 首先,如果needle为空,直接返回0;如果 haystack 为空 或者 haystack 的长度 小于 needle 的长度,直接返回-1;
  • 否则,从 haystack 的第一位开始跟 needle 进行匹配,如果匹配不上,则往后继续遍历haystack,直到遍历完成,就能得到结果。

说明:该方法效率比较差。

解法二:KMP算法

首先,构造一个next数组,先计算出下一次跳转的位置,然后遍历按照next数组的位置将原串和匹配串进行匹配。

import com.google.common.base.Strings;

public class LeetCode_028 {
/**
* 穷举法
*
* @param haystack
* @param needle
* @return
*/
public static int strStr(String haystack, String needle) {
if (needle == null || needle.length() == 0) {
return 0;
}
if (haystack == null || haystack.length() == 0 || haystack.length() < needle.length()) {
return -1;
}
int first = 0;
while (first < haystack.length()) {
int matchCount = 0;
for (int i = 0; i < needle.length() && (i + first) < haystack.length(); i++) {
if (needle.charAt(i) == haystack.charAt(i + first)) {
matchCount++;
} else {
break;
}
}
if (matchCount == needle.length()) {
return first;
} else {
first++;
}
}
return -1;
} /**
* KMP算法
*
* @param haystack 原串
* @param needle 匹配串
* @return
*/
public static int strStr2(String haystack, String needle) {
if (Strings.isNullOrEmpty(needle)) {
return 0;
}
// 分别获取原串和匹配串的长度
int haystackLength = haystack.length(), needleLength = needle.length();
// 原串和匹配串前面都加一个空格,使其下标从1开始
haystack = " " + haystack;
needle = " " + needle; char[] haystackList = haystack.toCharArray();
char[] needleList = needle.toCharArray(); // 构建 next 数组,数组长度为匹配串的长度(next 数组是和匹配串相关的)
int[] next = new int[needleLength + 1];
// 构造过程 i = 2, j = 0 开始,i 小于等于匹配串长度【构造 i 从 2 开始】
for (int i = 2, j = 0; i <= needleLength; i++) {
// 匹配不成功的话,j = next[j]
while (j > 0 && needleList[i] != needleList[j + 1]) {
j = next[j];
}
// 匹配成功的话,先让 j++
if (needleList[i] == needleList[j + 1]) {
j++;
}
// 更新 next[i],结束本次循环, i++
next[i] = j;
} // 匹配过程,i = 1, j = 0 开始,i 小于等于原串长度【匹配 i 从 1 开始】
for (int i = 1, j = 0; i <= haystackLength; i++) {
// 匹配不成功 j = next[j]
while (j > 0 && haystackList[i] != needleList[j + 1]) {
j = next[j];
}
// 匹配成功的话,先让 j++,结束本次循环后 i++
if (haystackList[i] == needleList[j + 1]) {
j++;
}
// 整一段都匹配成功,直接返回下标
if (j == needleLength) {
return i - needleLength;
}
} return -1;
} public static void main(String[] args) {
System.out.println(strStr("mississippi", "issi")); System.out.println(strStr2("mississippi", "issi"));
}
}

【每日寄语】 在最美的年华,做最喜欢的事情,别辜负了美好时光,借时光之手,暖一处花开,借一方晴空,拥抱梦想。

LeetCode-028-实现 strStr()的更多相关文章

  1. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  3. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  4. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  6. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

  7. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

  8. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  10. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

随机推荐

  1. 使用Xamarin开发移动应用示例——数独游戏(五)保存游戏进度

    项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu .代码随项目进度更新. 保存进度是移动应用的基本功能,在应用的使用过程中会有各种各样的可能导致使用中 ...

  2. Shiro 简单介绍和快速入门。

    一.shiro是啥? /* * Shiro ? 安全框架是一个 1.功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理,并可用于保护任何应用程序 - 从命令行应用程序,移动应用 ...

  3. APC 篇—— APC 执行

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...

  4. Linux性能优化实战(一)

    一.优化方向 1,性能指标 从应用负载的视角出发,考虑"吞吐"和"延时" 从系统资源的视角出发,考虑资源使用率.饱和度等 2,性能优化步骤 选择指标评估应用程序 ...

  5. 最大公因数与最小公倍数-gcd&lcm

    一.一些性质 \(gcd(a,b)=gcd(b,a)\) \(gcd(-a,b)=gcd(a,b)\) \(gcd(a,a)=|a|, gcd(a,0)=|a|\) \(gcd(a,1)=1\) \( ...

  6. CF1399F Yet Another Segments Subset

    首先注意一下题面要求,使得选出的线段两两要么包含要么不相交,也就是说一条线段可能会出现不相交的几条线段,而这些线段上面也可能继续这样包含线段.然后我们可以发现我们要做的实际上是在这条线段上选取几条线段 ...

  7. UIView属性的讲解

    1.父控件和子控件的理解在storyboard中只有UIView是可以在里面拖入子控件的,其他控件不可以(必须通过代码添加)拖入一个UIView控件,在里面添加一些子控件(UIView控件是控制器的V ...

  8. array_multisort array_merge 排序

    前段时间遇到一个排序问题,大致是这样的:$demo = array(        0        =>        array(                'name'         ...

  9. Mac版jdk1.6

    java sdk 1.6 for mac 在苹果官网下载 https://support.apple.com/kb/DL1572?locale=zh_CN

  10. onclick="func()"和 onclick = "return func()"区别

    onclick="func()" 表示只会执行 func , 但是不会传回 func 中之回传值onclick = "return func()" 则是 执行 ...