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 ...
随机推荐
- SQL 语句实战演练
1 创建数据库.删除数据库 备注:关键字不一定要大写. CREATE DATABASE sql_testDROP DATABASE sql_test 2 新建表 CREATE TABLE `emp` ...
- JavaScript之原型链与原型链继承
原型链 定义:每个实例对象(object)都有一个私有属性(称之为 __proto__ )指向它的构造函数的原型对象(prototype).该原型对象也有一个自己的原型对象(__proto__),层层 ...
- application/x-www-form-urlencoded、application/json、multipart/form-data、text/xml简单总结
最近在数据传输时,一直不明白这四种的区别,查了很多资料,也还是感到很模糊,因此,简单总结一下,以后再完善 1.在GET方式传输数据中,这四种格式,后台都可以接收数据(原生的request.getPar ...
- SSL证书,IIS7、IIS8,http自动跳转到HTTPS
安装"URL REWRITE2 " 伪静态模块,IIS7需要先确认是否安装 "URL REWRITE2 " 伪静态模块 , 如果您已经安装可以跳过 下载地址:h ...
- Td 内容不换行,超过部分自动截断,用...表示
转载请注明来源:https://www.cnblogs.com/hookjc/ <table width="200px" style="table-layout:f ...
- 在view中实现UIViewController的跳转 By H.L
view中是不能进行UIViewController的push,pop等操作的,若进行跳转操作,一般是用代理,block,通知等实现,那如何实现在ViewController的subView中实现跳转 ...
- 适用于小白:VSCode搭建Vue项目,最详细的搭建步骤哦
在vscode上搭建一个vue项目---初学总结. 1.假设Vscode.nodejs等已经安装好了. 2.全局安装vue-cli,vue-cli可以帮助我们快速构建Vue项目. 安装命令: npm ...
- 学习Spring5必知必会(1)~未使用spring前的麻烦
一.未使用spring前的麻烦 开闭原则:扩展是开放的,但是对于修改是"封闭的". 1.代码耦合度比较高[不符合开闭原则]: public class EmployeeServic ...
- tep集成mitmproxy录制流量自动生成用例
使用 操作过程非常简单,流程如下: ①配置过滤域名 必须配置,不然会有很多无效流量造成数据冗余. ②启动代理 「示例」使用了反向代理,tep自带FastApi启动Mock服务: 「实际」使用正向代理, ...
- ServiceStack.Redis的源码分析(连接与连接池)
前几天在生产环境上redis创建连接方面的故障,分析过程中对ServiceStack.Redis的连接创建和连接池机制有了进一步了解.问题分析结束后,通过此文系统的将学习到的知识点整理出来. 从连接池 ...