【leetcode】Implement strStr()
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;
}
};
|
a
|
b |
a
|
a
|
b
|
a
|
b
|
a
|
|
-1
|
0
|
0
|
1
|
1
|
2
|
3
|
2
|
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;
}
};
|
a
|
b |
a
|
a
|
b
|
a
|
b
|
a
|
|
-1
|
0
|
0
|
1
|
1
|
2
|
3
|
2
|
【leetcode】Implement strStr()的更多相关文章
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...
- 【Leetcode】【Easy】Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- sersync + rsync 实现文件的实时同步
这里有一点要特别注意了,就是在你完成备份之后,先不要把本地的文件都给删除了,先把服务停了之后再删除文件, 因为你已删除,检查到两边不一致,他又会把备份端给删除了.所以特别得注意了.这里吃过一次亏. 还 ...
- visual studio各个版本的差异
- BZOJ-1625 宝石手镯 01背包(傻逼题)
傻逼题,懒得打,复制蛋蛋的.. 1625: [Usaco2007 Dec]宝石手镯 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1076 Solved: ...
- jboss7 添加虚拟目录 上传文件路径
直接在jboss-as-7.1.1.Final\welcome-content\下加个子目录: jboss-as-7.1.1.Final\welcome-content\logs. 即可访问.
- ActivityInfo taskAffinity
通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- CSS3 动画animation
关键帧 什么是关键帧.一如上面对Flash原理的描述一样,我们知道动画其实由许多静态画面组成,第一个这样的静态画面可以表述为一帧.其中关键帧是在动画过程中体现了物理明显变化的那些帧. 比如之前的例子中 ...
- 三角形变形记之纯css实现的分布导航条效果
三角形变形记,用纯css实现的分布导航条效果 <style type="text/css"> ul,li { list-style-type:none; font-si ...
- ios 随机色 宏定义
#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #d ...
- gdb命令与调试方法
单线程 http://www.cnblogs.com/lidabo/p/5629830.html 编译程序一定要加-g选项 gcc -g test.c -o test 进入gdb调试:gdb 程序名 ...