LeetCode_implement strstr ()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
KMP :
class Solution {
public:
void calculatNext(char *pattern)
{
int i = , k = -;
next[] = -;
while( i < sizeNeed -) //计算next[i+1]
{
while(k >= && pattern[i] != pattern[k]) k = next[k];
i++; k++;
next[i] = pattern[i] == pattern[k] ? next[k] : k;
}
}
char *strStr(char *haystack, char *needle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sizeHay = strlen(haystack);
sizeNeed = strlen(needle);
if(sizeNeed == ) return haystack;
next.resize(sizeNeed);
calculatNext(needle);
int i = , j = ;
while(i< sizeHay && j < sizeNeed)
{
if(j == - || haystack[i] == needle[j]){
i++;j++;
}else
j = next[j];
}
if(j >= sizeNeed)
return haystack+(i - j);
else
return NULL ;
}
private :
int sizeHay;
int sizeNeed ;
vector<int> next ;
};
LeetCode_implement strstr ()的更多相关文章
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
- 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 ...
- LintCode StrStr
1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...
- strstr函数
原型:char * strstr( char *haystack, char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- Teach Yourself Scheme in Fixnum Days 6 recursion递归
A procedure body can contain calls to other procedures, not least itself: (define factorial (lambda ...
- CH Round #53-数据备份
描述 已知有N座办公楼位于同一条街上.你决定给这些办公楼配对(两个一组).每一对办公楼可以通过在这两个建筑物之间铺设网络电缆使得它们可以互相备 份.然而,网络电缆的费用很高.当地电信公司仅能为你提供K ...
- Android Spinner使用简介
Android中使用Spinner作为下拉列表,下面直接看实现方式: (1)使用ArrayAdapter来实现: 实现步骤: 1. 在布局文件中定义Spinner组件: 2. 向Spinner添加需要 ...
- PHP常见报错解析
{错误类型}: {错误原因} in {错误文件} on {错误行数} 说明了在哪个文件的哪一行中因何种原因出现了何种错误. 常见的错误类型一般有下面几种: Parse error(解析错误)一般都伴随 ...
- IT English Collection(9) of Objective-C
1 前言 今天我们来解除一篇有关Objective-C的介绍文章,详情如下. 2 详述 2.1 原文 Objective-C defines a small but powerful set of e ...
- Win32多线程编程(1) — 基础概念篇
内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- webpack的配置及使用
webpack 安装 命令行输入 npm install webpack 配置文件 webpack.config.js moudule.exports = { //Import 入口文件 entry: ...
- 关于transform的2D
在transform的学习中,自己总结了一点经验. 我们知道transform有2D和3D的两大类变换,这里分享下关于2D的属性简单示例: 一.2D变换: (x为水平,y为垂直) 1.skew(斜拉 ...