LeetCode-028-实现 strStr()
实现 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()的更多相关文章
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- 028 Implement strStr() 实现 strStr()
实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...
- 【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- linux虚拟化
虚拟化硬件虚拟化:一台物理机虚拟出多台逻辑上的计算机cpu,内存可分配给多个虚拟机 虚拟机中 guestos 的配置有什么用 内存越大越好,CPU也不要太差,如果要用64位虚拟系统或者是Hyper-V ...
- 开发升讯威在线客服系统启示录:怎样编写堪比 MSDN 的用户手册
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程. 免费在线使用 & 免费私有化部署:https://kf.shengxunwei.com 文章目 ...
- 「CTSC 2011」排列
「CTSC 2011」排列 要求不存在公差为 A 或者公比为 B 的子列,那么实际上可以把该问题转化为求一个图的最优拓朴序. 任意差为 A 或者比为 B 的两个数连一条边. 求一个合法序列的答案可以用 ...
- go 把固定长度的数字写入字节切片 (byte slice),然后从字节切片中读取到并赋值给一个变量:
// write v := uint32(500) buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, v) // read x := bin ...
- 新手应该如何学习 PHP 语言?
其实php开发,不只是一个简单的php开发,而是整个一个行业,一般叫web开发,或者php后端开发,所以从html,css,js,jq,php,sql基本这些都要有了解.当然你有html,css基础, ...
- jquery里面的$(this)和this的区别
感谢原文作者:何少旭 原文链接:https://www.cnblogs.com/heshaoxu/p/7672736.html 前言 当你用的是jquery时,就用$(this),如果是JS,就用th ...
- Spark算子 - reduce
释义 reduce将partition内所有记录最终计算成1个记录,结果类型与reduce 时数据类型一致 方法签名如下: def reduce(f: (T, T) => T): T = wit ...
- C#项目版本号自定义位置自动向上增加小工具设计与实现
自从毕了业,好久没更新了,今天突发奇想,过来更新一下,嘻嘻! 一般在做版本升级时,要锁定版本号进行对比,然后联网检索可用的升级包信息,在用VS做C#项目组件版本管理时,是一个很麻烦的事,每次Relea ...
- C#字符串Unicode转义序列编解码
C#字符串Unicode转义序列编解码 在开发过程中时常会遇到"\Uxxxx"格式表示的字符,实际上"xxxx"是字符的Unicode码的十六进制表示方式.这种 ...
- 我来教你如何将cpu使用率up起来(shell脚本[含注释])
这个脚本是为了逃过一些资源检测的,当一些机器当前使用率偏低,会被客户要求收回,那咋办呢?使用下面的脚本,就可以留住你的机器了 假设要求cpu使用率不能低于35% 使用方法:bash up_up_up. ...