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 ...
随机推荐
- <Spring Cloud>入门五 hystrix
1.服务熔断 1.1引入坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artif ...
- linux 如何查看硬盘大小,内存大小等系统信息及硬件信息
一.linux CPU大小[root@idc ~]# cat /proc/cpuinfo |grep "model name" && cat /proc/cpuin ...
- transformer模型解读
最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...
- git 项目相关
工具篇:Sourcetree 和 Git Bash Sourcetree Git一款非常好用的可视化工具,方便管理项目.下载地址 https://www.sourcetreeapp.com/ Git ...
- 剑指Offer(书):用两个栈实现队列
题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 分析:入栈时只入栈1,出栈为栈2:若栈2不为空,直接出栈:否则,将栈1中的值依次入栈2,之后栈2出栈 Sta ...
- luogu3168 [CQOI2015]任务查询系统
树状数组不用动脑子真爽啊 #include <algorithm> #include <iostream> #include <cstdio> using name ...
- python012 Python3 编程第一步
Python3 编程第一步在前面的教程中我们已经学习了一些 Python3 的基本语法知识,下面我们尝试来写一个斐波纳契数列.实例如下: #!/usr/bin/python3 # Fibonacci ...
- IBM MQ 创建以及常见问题集锦
消息队列+发送队列+消息通道 接收通道名称与发送端的发送通道名称要一致,修改通道信息后要执行 start channle(chlname) 重启通道.常用的MQ命令 66.0.42.240 用户 mq ...
- 【ITOO 4】WCF中,分布式事务处理
导读:事务可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源.通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠.在项目中,就有 ...
- HDU-2509-Be the Winner,博弈题~~水过~~
Be the Winner Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ht ...