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 ...
随机推荐
- Git - 使用命令和P4Merge进行diff
P4Merge P4Merge是Git的一个第三发Diff和Merge工具(可视化冲突解决工具). 下载地址: https://www.perforce.com/downloads/visual-me ...
- java例题_36 移动数组中数据位置(用到数组的合并操作)
1 /*36 [程序 36 移动位置] 2 题目:有 n 个整数,使其前面各数顺序向后移 m 个位置,最后 m 个数变成最前面的 m 个数,比如输入数字 3 为 1 2 3 4 5 6 7 8 9 0 ...
- 显示目录下的内容--ls
ls 显示当前目录下的所有文件或者文件夹,但不包括 . 和 .. ls -a 显示当前目录下的所有文件或者文件夹 ls -l ...
- HTML(一):语法结构
HTML语法规范 基本语法概述 HTML标签是由尖括号包围的关键词,例如<html>. 2HTML标签通常是成对出现的,例如<html>和</html> ,我们称为 ...
- BUAA_OO_第三单元
一.JML初探 JML(Java Modeling Language)作为一种形式化语言,可以约束Java代码中类和方法的状态和行为形成规格,通过将一系列具体代码实现抽象成明确的行为接口,可以形成 ...
- Java实现操作系统中四种动态内存分配算法:BF+NF+WF+FF
1 概述 本文是利用Java实现操作系统中的四种动态内存分配方式 ,分别是: BF NF WF FF 分两部分,第一部分是介绍四种分配方式的概念以及例子,第二部分是代码实现以及讲解. 2 四种分配方式 ...
- (一)Docker-in-Docker on Kubernetes
1. 场景 请参考docker in docker 文章 2. DinD 我们将采用主机Docker守护程序作为外部守护程序,Docker守护程序作为内部守护程序在容器内运行.运行DinD的一个重要方 ...
- 数据结构之Queue | 让我们一块来学习数据结构
前面的两篇文章分别介绍了List和Stack,下面让我们一起来学习Queue 数据结构之List | 让我们一块来学习数据结构 数据结构之Stack | 让我们一块来学习数据结构 队列的概况 队列是一 ...
- 031- 控制语句switch
语法 switch(表达式){ case 值1: java语句; break; case 值2: java语句; break; case 值3: java语句; break; default: jav ...
- POJ 1386 欧拉路的判定
题意: 给你n个单词,问你有没有一种排列方式可以所有单词的首部是相邻单词的尾部. 思路: 这个题目还挺基础的,就是个欧拉的判定,首先对于每一个单词,我们把他抽象成边,每个单词两 ...