【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 ...
随机推荐
- Java基础-基本数据类型转换案例
java基本数据类型八中 byte = Byte short = Short char = Character int = Integer long = Long float = Float doub ...
- Java 并发编程 Executor
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- 左偏树(DP)问题
问题:A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ woul ...
- BZOJ-3669 魔法森林 Link-Cut-Tree
意识到背模版的重要性了,记住了原理和操作,然后手打模版残了..颓我时间...... 3669: [Noi2014]魔法森林 Time Limit: 30 Sec Memory Limit: 512 M ...
- BZOJ-1008 越狱 数论快速幂
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 6192 Solved: 2636 [Submit][Status] ...
- BZOJ1086 [SCOI2005]王室联邦
Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成 员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个 ...
- gotoTop返回顶部 JS
方法: 1.首先在body添加一个标签,在一个页面添加,其它页面也会生效. <body> <a name="top"> 2.然后在页脚添加一个链接 < ...
- Linux系统如何查看版本信息
输入"uname -a ",可显示电脑以及操作系统的相关信息. 输入"cat /proc/version",说明正在运行的内核版本. 输入"c ...
- MVC 返回图片
//调用 http://localhost:60663/home/GetCoder39Img?mycode=123443545 public void GetCoder39Img(string myc ...
- MyEclipse------快速读取特定目录下的文件的内容(字节输入流)
other.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...