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 ()的更多相关文章

  1. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  4. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  7. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

随机推荐

  1. Android 获取文件大小

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位 FileSizeUtil public class FileSizeUtil { ;//获取文件大小单位为B的double值 ; ...

  2. jQuery的animate方法在IE7下出现小问题

    接上,今天接着修改网站的bug,还是网页的这几张图片,还是滑动效果,但在IE7下不但几张图片只显示一张,更别提滑动效果了,根本滑不了啊: 然后打开IE的F12工具,先看样式,再看滑动效果:本来应该显示 ...

  3. 【绿茶书情】:《SOHO小报》和《凤… - 绿茶的日志 - 网易博客

    [绿茶书情]:<SOHO小报>和<凤- - 绿茶的日志 - 网易博客 [绿茶书情]:<SOHO小报>和<凤-  

  4. hdu 5159 Card (期望)

    Problem Description There are x cards on the desk, they are numbered from 1 to x. The score of the c ...

  5. JPA字段映射(uuid,日期,枚举,@Lob)

    转:http://www.cnblogs.com/tazi/archive/2012/01/04/2311588.html 主键: JPA主键的生成策略不像Hibernate那么丰富. @Id @Ge ...

  6. Css实现垂直水平居中的六种方法

    经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 6 ...

  7. HeadFirst设计模式 之 C++实现(二):Observer(观察者模式)

    观察者模式是最经常使用的设计模式之中的一个,[对象之间多对一的依赖关系,当一个对象发生变化时,其会通知全部依赖它的对象].拿订阅报纸和发行报社打例如,报社採集到news制作新的报纸,派送给订阅的客户. ...

  8. SELECT--UNION,UNION ALL,MINUS, INTERSECT,EXISTS

    SELECT--UNION,UNION ALL,MINUS, INTERSECT返回两个查询结果的集合操作,两个查询结果集必须字段相同.UNION和UNION ALL并集操作,UNION并集后去掉重复 ...

  9. python - 类成员修饰符

    在java,c#类的成员修饰符包括,公有.私有.程序集可用的.受保护的. 对于python来说,只有两个成员修饰符:公有成员,私有成员 成员修饰符是来修饰谁呢?当然是修饰成员了.那么python类的成 ...

  10. .net中的特性

    本文来之:http://hi.baidu.com/sanlng/item/afa31eed0a383e0e570f1d3e 在一般的应用中,特性(Attribute,以称为属性)好像被使用的不是很多. ...