leecode之Implement strStr()
KMP算法的实现:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> int strStr(char* haystack, char* needle) {
if (haystack == NULL || needle == NULL)
return -1;
if (needle[0] == '\0')
return 0;
int lenPattern = strlen(needle);
int lenHaystack = strlen(haystack);
int *shift = (int *)malloc(sizeof(int) * lenPattern);
int i;
shift[0] = 0; //for (i = 1; i < lenPattern; i++){
// if (needle[shift[i - 1]] == needle[i])
// shift[i] = shift[i - 1] + 1;
// else
// {
// if (needle[i] == needle[0])//这里有bug,但是leecode通过了,原因是刚好没碰到过不了的测试案例
// shift[i] = 1;
// else
// shift[i] = 0;
// }
//} //代码改写如下:
int j = 0;
shift[0] = j;
for (i = 1; i < lenPattern;) {
if (needle[i] == needle[j])
{
j++;
shift[i] = j;
i++;
}
else
{
if (j == 0)
{
shift[i] = 0;
i++;
}
else
{
j = shift[j - 1];
} }
} j = 0;
for (i = 0; i < lenHaystack;)
{
if (haystack[i] == needle[j])
{
i++;
j++;
if (j == lenPattern)
return (i - j);
}
else
{
if (j == 0)
{
i++;
}
else
{
j = shift[j - 1];
} }
}
return -1;
} int main()
{
//char *input = "";
//char *pattern = "";
char *input = "BBC ABCDAB ABCDABCDABDE";
char *pattern = "ABCDABD";
printf("%d\n", strStr(input, pattern)); return 0;
}
leecode之Implement strStr()的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
随机推荐
- 使用C# (.NET Core) 实现装饰模式 (Decorator Pattern) 并介绍 .NET/Core的Stream
该文章综合了几本书的内容. 某咖啡店项目的解决方案 某咖啡店供应咖啡, 客户买咖啡的时候可以添加若干调味料, 最后要求算出总价钱. Beverage是所有咖啡饮料的抽象类, 里面的cost方法是抽象的 ...
- Android Studio 之 通过 Intent 完成点击按钮实现页面跳转
•Intent 简介 Intent 是 Android 程序中各组件之间进行交互的一种重要方式: 它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. Intent 有多个构造函数,其 ...
- Kafka 常见问题汇总
Kafka 常见问题汇总 1. Kafka 如何做到高吞吐.低延迟的呢? 这里提下 Kafka 写数据的大致方式:先写操作系统的页缓存(Page Cache),然后由操作系统自行决定何时刷到磁盘. 因 ...
- [状压DP]子矩阵
子 矩 阵 子矩阵 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵 如,下面左图中选取第 2 . 4 ...
- [Fundamental of Power Electronics]-PART I-4.开关实现-4.3 开关损耗/4.4 小结
4.3 开关损耗/4.4 小结 使用半导体器件实现开关后,我们现在可以讨论变换器中损耗和低效的另一个主要来源:开关损耗.如前所述,半导体器件的导通和关断转换需要几十纳秒到几微秒的时间.在这些开关转换期 ...
- CSS3新增了哪些新特性
一.是什么 css,即层叠样式表(Cascading Style Sheets)的简称,是一种标记语言,由浏览器解释执行用来使页面变得更为美观 css3是css的最新标准,是向后兼容的,CSS1/2的 ...
- Java(94-100)【数组、对象】
1.数组作为方法的参数 任何数据类型都可以作为方法的参数 直接建立数组的方法,将数组作为方法的参数. 当调用方法的时候传递的是数组的地址. 2.数组作为方法的返回值 任何数据类型都可以作为方法的参数, ...
- redis的持久化有哪几种方式?不同的持久化机制都有什么优缺点?(偏难)
1.RDB和AOF两种持久化机制的介绍 RDB持久化机制,对redis中的数据执行周期性的持久化 AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的 ...
- 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像上传-11
自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像上传-11 欢迎加QQ群:1026880196 进行交流学习 镜像上传 #controller1 ...
- java7与java9中的try-finally关闭资源
1.java7中的try 在java7之前,对于一些需要使用finally关闭资源的操作,会显得很臃肿. try { // } catch(Exception e) { // } finally { ...