Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

最简单的思路,逐一比较:
 class Solution {
public:
int strStr(char *haystack, char *needle) { int n1=strlen(haystack);
int n2=strlen(needle);
int result=-;
bool flag;
for(int i=;i<n1-n2+;i++)
{
flag=true;
for(int j=;j<n2;j++)
{
if(haystack[i+j]==needle[j])
{
continue;
}
else
{
flag=false;
break;
}
} if(flag==true)
{
result=i;
break;
}
}
return result;
}
};
 
可以采用KMP算法:
KMP算法的核心是,当比较某两个字符不相等时,不必从头开始重新比较,而是根据引入的next,确定子串回溯的位置。
 
 
举个例子:
 
一个子串:

a
b
a
a
b
a
b
a
-1
0
0
1
1
2
3
2
 
next[j]表示了如果在比较字符串时,needle[j]!=haystack[k]
那么下一次比较时,从next[j]所指定的位置进行比较,即j=next[j],k不变
 
 
 
 
 
 class Solution {
public:
int strStr(char *haystack, char *needle) { int i=;
int j=;
int n1=strlen(haystack);
int n2=strlen(needle); vector<int> next=getNext(needle); while(i<n1&&j<n2)
{
if(j==-||haystack[i]==needle[j])
{
i++;
j++;
}
else
{
j=next[j];
}
} if(j==n2)
{
return i-j;
}
else
{
return -;
}
} vector<int> getNext(char *needle)
{
int n=strlen(needle); vector<int> next(n);
if(n==)
{
return next;
} next[]=-; int i=;
int k=-;
while(i<n-)
{
if(k==-||needle[k]==needle[i])
{ i++;
k++;
next[i]=k;
}
else
{
k=next[k];
}
} return next; } };
 
 对于k=next[k]
举个例子:如果比较最
a
b
a
a
b
a
b
a
-1
0
0
1
1
2
3
2
 
最后一个元素的next为2
这是因为,倒数第二个元素与第四个元素不相等,此时
k=next[3]=1;
 
继续循环,
b==needle[k]=needle[1]
 
所以
i++
k++
得到
next[7]=2;
 
 
 

【leetcode】Implement strStr()的更多相关文章

  1. 【leetcode】Implement strStr() (easy)

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

  2. 【LeetCode】Implement strStr()(实现strStr())

    这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...

  3. 【Leetcode】【Easy】Implement strStr()

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

  4. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  5. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  6. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  7. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  8. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. js库中$冲突的解决方法

    http://www.w3school.com.cn/jquery/core_noconflict.asp

  2. 【BZOJ 3036】 绿豆蛙的归宿

    求期望的题目(~~~water~~~) 压了下代码,压成15行hhh: 我把代码压成这么丑估计也没有人看吧: 毕竟是zky讲的一个水题,就当给博客除草了:    dfs回溯时求当前节点的f,除以当前节 ...

  3. 【OpenJudge1814】 恼人的青蛙 暴力+剪枝优化

    此题poj1054上也有 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  4. BZOJ-1196 公路修建问题 最小生成树Kruskal+(二分??)

    题目中一句话,最大费用最小,这么明显的二分的提示(by 以前morestep学长的经验传授)...但完全没二分,1A后感觉很虚.. 1196: [HNOI2006]公路修建问题 Time Limit: ...

  5. poj 3734 矩阵快速幂+YY

    题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...

  6. phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...

  7. 嵌入式实时操作系统μCOS原理与实践+事件部分代码

    //事件等待表的初始化函数:pevent表示事件控制块的指针#if (OS_EVENT_EN)void  OS_EventWaitListInit (OS_EVENT *pevent){    INT ...

  8. sourceinsight安装记录

    sourceinsight安装记录 此文章为本人使用sourceinsight一个星期之后的相关设置步骤记录和经验记录,以备以后查验,网上的相关资料都也较为完善,但是对于新手还是有一定困难的,所以在这 ...

  9. Redis 下载

    https://github.com/MSOpenTech/redis/releases下载 Redis伴侣Redis Desktop Manager:http://redisdesktop.com/ ...

  10. IE兼容模式下两个小问题,JSON.stringify和SCRIPT70 无权限

    JSON.stringify在IE兼容模式下不起作用,原来是序列化对象是一个easyuiTree的树节点对象,过于复杂的对象 SCRIPT70 权限,问题出现在获取页面iframe时: var ifr ...