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. 【BZOJ 3223】文艺平衡树 模板题

    就是打个翻转标记,下推标记时记得交换左右孩子指针,查询kth和中序遍历输出时也记得要下推标记同时交换指针,二者不可缺!←这是易错点 仿陈竞潇学长模板的代码: #include<cctype> ...

  2. .Net身份验证概述

    一直以来,所有的系统基本都会有用户的登陆验证过程,整个过程其实也不难理解,就是对于cookie的解析.微软的.Net平台围绕用户身份验证授权也有好几个版本了,从早期的Membership到Identi ...

  3. 只用js 实现的简约聊天框

    之前看到别人的网页打开后都有个聊天框,可以与同时在网上的网友聊点简单话题,于是便找了个最简单的方法 使用js,客户端 打开页面,不断的给服务器发送请求来得到 新的消息 用JavaScript实现的轮询 ...

  4. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

  5. 34.Android之资源文件res里drawable学习

    我们经常看到android工程资源文件res下drawable如ldpi.mdpi.hdpi.xhdpi.xxhdpi文件,今天我们学习了解下. (1)drawable-hdpi里面存放高分辨率的图片 ...

  6. [Unity 游戏设计的元素]

    1.核心游戏机制 2.主题 3.功能集合 4.可能的附加功能 5.备用主题创意

  7. ActionBar右边菜单按钮的添加

    在res目录下新建文件夹menu,存放men.xml文件 menu.xml <menu xmlns:android="http://schemas.android.com/apk/re ...

  8. Java Web文件上传

    参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...

  9. 数字、大写字母、小写字母,谁的ASCII码最大?

    常见ASCII码的大小规则:0~9<A~Z<a~z几个常见字母的ASCII码大小: “A”为65:“a”为97:“0”为 48.

  10. motto4

    有时候,你不能太固执,因为这样子对你不利,应该懂得变通才行. 你要知道,语言是表达思想的工具.你不说,别人怎么知道你的思想呢?你又怎么了解他人的思想呢?