【Leetcode】【Easy】Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解题:
本题为典型的KMP算法考察题,KMP算法描述为:
设主串S,匹配串P,i为S的索引下标,j为P的索引下标,初始i和j设置为0。
在i小于S的长度和j小于P的长度时,开始循环:
1、比较S[i]和P[j]是否相同;
2、如果相同则i++,j++,返回执行第1步;
3、如果不同,则计算已匹配成功的P[0]~P[j-1]中,相同前缀和后缀的最大长度,设为n;
4、令i不变,j为n(指向相同前后缀的后一个字符),返回执行第1步;
循环结束时,查看j值是否等于P的长度,如果等于则匹配成功,否则主串中不包含匹配串。
计算最长相同前后缀:
新建一个数组a,数组长度为匹配串P长度。数组的每一位a[i],表示由P[0]~P[i]表示的字符串,最长相同前后缀的位数;
a[0]初始化为0,令i为1,j为0,对于a[i](0<i<len)有两种情况:
1、如果P[j] == P[i],那么a[i] = a[i - 1] + 1;
接着j++,i++重新执行第一步;
2、当P[j] != P[i],如果j此时为0,表示由P[0]~P[i]组成的字符串没有相同的前缀和后缀,所以a[i]=0,i++继续进行第一步;
3、当P[j] != P[i],并且j不为0,表示可能包含相同前缀和后缀,则令j = a[j - 1],继续执行第一步;
直到计算出所有a[i]的值。
AC代码见:
class Solution {
public:
int strStr(char *haystack, char *needle) {
int num = strlen(needle);
int *next = new int[num];
getNext(needle, next); int i = ;
int j = ;
while (haystack[i] != '\0' && needle[j] != '\0') {
if (haystack[i] == needle[j]) {
++i;
++j;
} else if (j == ) {
++i;
} else {
j = next[j - ];
}
} free(next);
if (needle[j] != '\0')
return -;
else
return i - j;
} void getNext(char *needle, int *next) {
int i = ;
int j = ;
next[] = ;
while (needle[i] != '\0') {
if (needle[i] == needle[j]) {
next[i] = j + ;
++i;
++j;
} else if (j == ) {
next[i] = ;
++i;
} else {
j = next[j - ];
}
}
}
};
代码优化:
实际编写中,为了避免判定j是否为0,简化操作。可以设定next数组,取代a数组。next的含义是,当kmp算法需要寻找子串下一个比较的位置时,直接从next数组中取值;
其中next[0] = -1作为哨兵位,next[i] = a[i - 1],即将a数组整体后移一位保存在next数组中。
AC代码如下:
class Solution {
public:
int strStr(char *haystack, char *needle) {
int num = strlen(needle);
int *next = new int[num];
getNext(needle, next); int i = ;
int j = ;
while (haystack[i] != '\0' && j < num) {
if (j == - || haystack[i] == needle[j]) {
++i;
++j;
} else {
j = next[j];
}
} free(next);
if (needle[j] != '\0')
return -;
else
return i - j;
} void getNext(char *needle, int *next) {
int i = ;
int j = -;
int strl = strlen(needle);
next[] = -;
while (i < strl - ) {
if (j == - || needle[i] == needle[j]) {
next[++i] = ++j;
} else {
j = next[j];
}
}
}
};
【Leetcode】【Easy】Implement strStr()的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- 【LeetCode算法-28/35】Implement strStr()/Search Insert Position
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
随机推荐
- 洛谷 P3380 【模板】二逼平衡树(树套树)
题面 luogu 题解 2019年AC的第一道题~~ 函数名命名为rank竟然会ce 我写的是树状数组套值域线段树(动态开点) 操作1:询问\(k\)在\([l-r]\)这段区间有多少数比它小,再加\ ...
- 洛谷 P1155 【NOIP2008】双栈排序
题目链接 题解 这题有点神啊.. 我们仔细观察一下,发现两个栈内元素必须为降序 那么有结论 如果有\(i < j < k\) 且 \(a[k] < a[i] < a[j]\)则 ...
- 洛谷 P1453 城市环路 ( 基环树树形dp )
题目链接 题目背景 一座城市,往往会被人们划分为几个区域,例如住宅区.商业区.工业区等等.B市就被分为了以下的两个区域--城市中心和城市郊区.在着这两个区域的中间是一条围绕B市的环路,环路之内便是B市 ...
- cmd命令行编译c/c++程序
一.打开文件夹(文件夹名字为123,文件夹里面有程序345.cpp) cd 123 二.在cmd里面编译程序 1.cd Desktop //在打开文件夹之前不回到桌面会出错 2.cd 123 ...
- 2019.3.25 SQL语句(进阶2)
子查询 数据库中的表沿用 上一篇博客 中使用的Employee1. 练习: 1.求所有年龄比张三小的人 select * from Employee1 where age < (select a ...
- EntityFramework 建立一对一关系
前言:本来要使用实体拆分实现一对一,但发现查询时无法单独查询,影响效率,故改用手动建立一对一关系 例: 实体类: public class TestDbContext : DbContext { pu ...
- Linux误挂载到根目录出现问题!!!!!!!!!!!!!!!
一.背景: 因根目录/空间不大,故而想将另一硬盘挂载到根目录下(后发现此想法很是幼稚): 二.过程: 1.成功输入命令挂载后,发现出现/上被挂了两个东西,且/下剩余空间还是原来一样大,才发现大错特错: ...
- mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)
1.使用相同索引键值的冲突 由于mysql 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但如果是使用相同的索引键,是会出现锁冲突的.设计时要注意 例如:city表city_ ...
- method reference
import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util ...
- (转)linux sed命令就是这么简单
sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...