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. webservice的几个简单概念

    1.什么是JAX-WS? http://baike.baidu.com/view/1865210.htm?fr=aladdin 2.什么是JAX-RPC? http://baike.baidu.com ...

  2. 【转】android 5.0 64bit系统加载库文件失败问题浅析

    原文网址:http://blog.csdn.net/andrewblog/article/details/43601303 最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本 ...

  3. 轻量级开源嵌入式关系数据库sqlite基本使用及接口初识

    preface,先闲来扯下蛋: 嵌入式数据库,NoSQL的是BerkeleyDB和InnoDB,leveDb.TC(个人较不熟悉),关系型嵌入式是SQLite; 服务器性质的NoSQL服务器,如Red ...

  4. Install and configure sql server 2008 express

    http://www.symantec.com/connect/articles/install-and-configure-sql-server-2008-express

  5. WPF 实现控件间拖拽内容

    想实现这样一个常用功能:在ListBox的一个Item上点住左键,然后拖拽到另外一个控件(如ListView中),松开左键,数据已经拖拽过来. 步骤如下: 1. 设置ListBox 的AllowDro ...

  6. java 读取mysql库表数据

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  7. python数据类型—列表(增改删查,统计,取值,排序)

    列表是最常用的数据类型之一,通过列表可以对数据实现方便的存储,修改等操作. 先声明一个空列表: >>> names = [] >>> names [] 可以存多个值 ...

  8. C/C++经典面试题目

    1.关于动态申请内存 答:内存分配方式三种: (1)从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.全局变量,static变量. (2)在栈上创建:在执行函数 ...

  9. JSP简单练习-数组应用实例

    <%@ page contentType="text/html; charset=gb2312" %> <html> <body> <% ...

  10. STM32外部中断具体解释

      一.基本概念 ARM Coetex-M3内核共支持256个中断,当中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32眼下支持的中断共84个(16个内部+68个外部), ...