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 ...
随机推荐
- 数据结构&&算法基础知识
写本篇主要是为了将基础知识梳理一遍,天天加一些基本东西,以后复习时可以返回来看看. 数据结构&&基础算法: 基本算法: 二分查找 二叉树: 二叉树的各种遍历 位操作: 排序: 排序算法 ...
- TVS管
1.原理 TVS二极管在线路板上与被保护线路并联,当瞬时电压超过电路正常工作电压后,TVS二极管便产生雪崩,提供给瞬时电流一个超低电阻通路,其结果是瞬时电流透过二极管被引开,避开被保护元件,并且在电压 ...
- java实现二维码
说起二维码,微信好像最先启用,随后各类二维码就开始流行起来了.那什么是二维码呢. 1.什么是二维码?百度一下即可 http://baike.baidu.com/view/132241.htm?fr=a ...
- bzoj 1192
http://www.lydsy.com/JudgeOnline/problem.php?id=1192 好像学过一个东西: [0..2^(N+1)-1]内的数都的都可以由2^0,2^1,...,2^ ...
- c#发送邮件样例
1.通过gmail邮箱发送邮件 try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(& ...
- 多重背包问题的两种O(M*N)解法
多重背包的题目很多,最著名的是poj1742楼教主的男人八题之一. poj1742:coins 有几种面值的钱币和每种的数量,问能够组成m以内的多少种钱数 这个题大家都归为多重背包问题,不过跟实际意义 ...
- sphinx全文检索之PHP使用教程
以上一篇的email数据表为例: 数据结构: 01.CREATE TABLE email ( 02.emailid mediumint(8) unsigned NOT NULL auto_increm ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- 初识QML学习机制
在QML中,一个用户界面被指定为具有属性的对象树,这使得Qt更加便于很少或没有编程经验的人使用,JavaScript在QML中作为一种脚本语言,对QML进行逻辑方面的编程. AD:WOT2015 互联 ...
- 安装oracle11g未找到文件WFMLRSVCApp.ear文件
win7_64位系统,安装oracle11gR2时,报错提示: 未找到文件...WFMLRSVCApp.ear文件 解决方法如下: 将下载的两个压缩包解压至同一目录(合并)再安装即可解决此类问题.