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. 小qyvlik 先看两个视频,和 QtQuick UI 问答

    http://edu.csdn.net/course/detail/1042 http://edu.csdn.net/course/detail/335 http://blog.csdn.net/qy ...

  2. Polymorphism & Overloading & Overriding

    In Java, a method signature is the method name and the number and type of its parameters. Return typ ...

  3. PhoneGap 和 PhoneGap Build 是什么?

    PhoneGap是目前唯一支持7种平台的开源移动开发框架,支持的平台包括iOS.Android.BlackBerry OS.Palm WebOS.Windows Phone 7.Symbian和Bad ...

  4. Qt 界面使用自己定义控件 &quot;提升为&quot;

    1.效果图 我做了一个很easy的样例,一个能够显示颜色的QLabel,边上有个button,点击,跳出颜色选取的Dialog,然后选择一个颜色.这个QLabel会变成什么颜色. 2.ColorLab ...

  5. TBB入门

    获取TBB TBB的官方网站在http://threadingbuildingblocks.org/,可以在它的Downloads页面里找到Commercial Aligned Release,最新版 ...

  6. js数字精度丢失

    http://www.cnblogs.com/snandy/p/4943138.html

  7. SVG 路径(path)

    本文转自:https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths <path>元素是SVG基本形状中最强大的一个,它 ...

  8. 做量化模型Matlab、R、Python、F#和C++到底选择哪一个?

    MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室).是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它将数 ...

  9. OkHttp 上手

    OkHttp 上手 优点 快.节省带宽. 支持 HTTP/2 和 SPDY. HTTP/2 和 SPDY 允许对同一个主机的所有请求,使用一个 socket. 如果不支持 SPDY 的话,可以用连接池 ...

  10. oracle to_char()及to_date()函数使用

    to_char(x[,format]) :将x转换成字符串,可以使用format参数来格式化字符串输出. to_date(x[,format]) :将字符串x转换成日期,可以使用format匹配要转换 ...