当前位置:网站首页>字符串——28. 实现 strStr()
字符串——28. 实现 strStr()
2022-07-24 13:53:00 【向着百万年薪努力的小赵】
1 题目描述
- 实现 strStr()
实现 strStr() 函数。
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。
2 题目示例
示例 1:
输入:haystack = “hello”, needle = “ll”
输出:2
示例 2:
输入:haystack = “aaaaa”, needle = “bba”
输出:-1
3 题目提示
1 <= haystack.length, needle.length <= 104
haystack 和 needle 仅由小写英文字符组成
4 思路
本题是经典的字符串单模匹配的模型,因此可以使用字符串匹配算法解决,常见的字符串匹配算法包括暴力匹配、Knuth-Morris-Pratt算法、Boyer-Moore算法、Sunday 算法等,本文将讲解Knuth-Morris-Pratt算法。
因为哈希方法可能出现哈希值相等但是字符串不相等的情况,而strStr函数要求匹配结果必定正确,因此本文不介绍哈希方法,有兴趣的读者可以自行了解滚动哈希的实现(如Rabin-Karp算法)。
方法一:暴力匹配思路及算法
我们可以让字符串needle 与字符串haystack的所有长度为m的子串均匹配一次。
为了减少不必要的匹配,我们每次匹配失败即立刻停止当前子串的匹配,对下一个子串继续匹配。如果当前子串匹配成功,我们返回当前子串的开始位置即可。如果所有子串都匹配失败,则返回—1。
复杂度分析
时间复杂度:o(n x m),其中n是字符串hagystack的长度,m是字符串needle的长度。最坏情况下我们需要将字符串needle与字符串haystack的所有长度为m的子串均匹配一次。
空间复杂度:O(1)。我们只需要常数的空间保存若干变量。
方法二:KMP
介绍不说了,太长,大家可以自己搜索一下
5 我的答案
方法一:暴力
class Solution {
public int strStr(String haystack, String needle) {
int n = haystack.length(), m = needle.length();
for (int i = 0; i + m <= n; i++) {
boolean flag = true;
for (int j = 0; j < m; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return i;
}
}
return -1;
}
}
方法二:KMP
class Solution {
public int strStr(String haystack, String needle) {
int n = haystack.length(), m = needle.length();
if (m == 0) {
return 0;
}
int[] pi = new int[m];
for (int i = 1, j = 0; i < m; i++) {
while (j > 0 && needle.charAt(i) != needle.charAt(j)) {
j = pi[j - 1];
}
if (needle.charAt(i) == needle.charAt(j)) {
j++;
}
pi[i] = j;
}
for (int i = 0, j = 0; i < n; i++) {
while (j > 0 && haystack.charAt(i) != needle.charAt(j)) {
j = pi[j - 1];
}
if (haystack.charAt(i) == needle.charAt(j)) {
j++;
}
if (j == m) {
return i - m + 1;
}
}
return -1;
}
}
边栏推荐
- How to quickly wrap lines in Excel table
- Soft link, hard link
- Network security - Cookie injection
- Network security -- man in the middle attack penetration test
- R语言使用epiDisplay包的statStack函数基于因子变量通过分层的方式查看连续变量的统计量(均值、中位数等)以及对应的假设检验、对连续数据进行对数化之后符合参数检验条件自动执行参数检验
- Flink综合案例(九)
- 使用树莓派做Apache2 HA实验
- 2022.7.22 模拟赛
- 自动化运维之Ansible安装部署
- rhce第一次作业
猜你喜欢

OWASP ZAP安全测试工具使用教程(高级)

Network security - function bypass injection

OWASP zap security testing tool tutorial (Advanced)

网络安全——Cookie注入

Network security - file upload competitive conditions bypass

网络安全——文件上传渗透测试

Swarm intelligence collaborative obstacle avoidance method inspired by brain attention mechanism

软链接、硬链接

Aggregation measurement of robot swarm intelligence based on group entropy

网络安全——函数绕过注入
随机推荐
OWASP zap security testing tool tutorial (Advanced)
Network security - error injection
Network security - Web penetration testing
Aggregation measurement of robot swarm intelligence based on group entropy
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
One problem encountered:
Network security - file upload competitive conditions bypass
在EXCEL表格中如何进行快速换行
Network security - file upload content check bypass
Flink advanced features and new features (VIII)
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.Y.axis参数指定Y轴分组标签文本的大小
关于不定方程解的个数的问题
Network security - file upload penetration test
在LNMP架构中搭建Zabbix监控服务
Flex layout
Csp2021 T3 palindrome
Network security - filtering bypass injection
网络安全——文件上传白名单绕过
代码签名证书与SSL证书区别
数据类型二进制字符串类型