leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
这个题考查的是KMP算法。先求特征向量,然后再进行匹配,确实能够大大提高效率。code例如以下:
class Solution {
public:
char *strStr(char *haystack, char *needle) {
if(strlen(haystack)==0&&strlen(needle)==0)
return haystack;
if(strlen(haystack)==0&&strlen(needle)!=0)
return NULL;
if(strlen(needle)==0)
return haystack;
int m=strlen(needle);
int *N = new int[m];
N[0]=0;
int i,j,k;
for(i=1;i<m;i++)
{
k=N[i-1];
while(k>0 && needle[i]!=needle[k])
{
k=N[k-1];
}
if(needle[i]==needle[k])
N[i]=k+1;
else
N[i]=0;
}
j=0;
for(i=0;i<strlen(haystack);i++)
{
while(j>0 && needle[j]!=haystack[i])
j=N[j-1];
if(needle[j]==haystack[i])
j++;
if(j==strlen(needle))
return haystack+i-j+1;
}
return NULL;
}
};
leetcode——Implement strStr() 实现字符串匹配函数(AC)的更多相关文章
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- python实现 字符串匹配函数
通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
随机推荐
- luogu P1364 医院设置
题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为1.如上 ...
- bzoj3336 Uva10572 Black and White
题目描述: 数据范围:2<=n,m<=8 题解: 很明显需要状压.但是怎么压不知道,压什么不知道. 然后从条件下手. 条件1要求黑色在一起白色在一起,记录轮廓线很容易做到. 条件2要求不能 ...
- [LUOGU] [NOIP2017] P3960 列队
题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n \times mn×m 名学生,方阵的行 ...
- PHP将数据库的数据转换成json格式
header('content-type:application/json;charset=utf8'); $results = array(); while ($row = mysql_f ...
- 解决Spellchecker inspection helps locate typos and misspelling in your code
idea出现这个是因为词库中没有这个单词,所以提示拼写错误 解决办法:双击下面有虚线的单词——>鼠标右键——>spelling——>save 'xxx' to distionary
- Linux基础测试
目 录 第1章 文件及目录课后作业 1 第2章 Linux打包与压缩习题 1 第3章 Linux系统VIM编辑器习题 1 文件及目录课后作业 从/proc/meminfo中过滤出 ...
- JavaScript:对Object对象的一些常用操作总结
JavaScript对Object对象的一些常用操作总结. 一.Object.assign() 1.可以用作对象的复制 var obj = { a: 1 }; var copy = Object.as ...
- react深入 - 手写实现react-redux api
简介:简单实现react-redux基础api react-redux api回顾 <Provider store>把store放在context里,所有子组件可以直接拿到store数据 ...
- 第三讲:post-processsing with vcs+ files
1,dump wave by system function $vcdpluson(level_number,module_instance,....|net_or_reg) $vcdplusoff ...
- Mac OS X 上如何切换默认的 Python 版本?