方法一:暴力解法

 int strStr(string haystack, string needle) {
if (needle.empty())
return ; int M = haystack.size();
int N = needle.size(); for (int i = ; i <= M - N; i++)
{
int j;
for (j = ; j < N; j++)
{
if (needle[j] != haystack[i + j])
{
break;
}
}
if (j == N)
return i;
}
return -;
}

方法二:利用memcmp,一层for循环即可解决

 int strStr(string haystack, string needle) {
if (needle.empty())
return ; int M = haystack.size();
int N = needle.size(); char* m = new char[M + ];
char* n = new char[N + ]; memcpy(m, haystack.c_str(), M);
memcpy(n, needle.c_str(), N);
m[M] = '\0';
n[N] = '\0'; for (int i = ; i <= M - N; i++)
{
if ( == memcmp(&m[i], n, N))
{
delete[] m;
delete[] n;
return i;
}
} delete[] m;
delete[] n; return -;
}

方法三: KMP算法

实现strStr()函数的更多相关文章

  1. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  2. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  3. C语言中strstr函数

    头文件:#include <string.h> strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:    char *strstr( char *str, char * ...

  4. strstr 函数用法

    strstr 编辑 strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. C语言函数 编辑 ...

  5. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  6. strstr函数与strcmp函数

    1.strstr函数主要完成在一个字串中寻找另外一个字串 函数实现工程如下:摘自http://baike.baidu.com/link?url=RwrzOxs0w68j02J2uQs5u1A56bEN ...

  7. C strstr() 函数

    包含文件:string.h 函数名: strstr 函数原型:extern char *strstr(const char *str1, const char *str2); 语法:* strstr( ...

  8. C语言strstr()函数:返回字符串中首次出现子串的地址

    今天又学到了一个函数 头文件:#include <string.h> strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:    char *strstr( char *s ...

  9. 使用PHP的strstr()函数来统计一段字符串中元音字母的个数(区分大小写)

    <?php/**练习:统计一段字符串中所有元音字母的个数(区分大小写)*/$str='This is a test file.'; //原始字符串echo $str.'<br>'; ...

  10. strstr()函数实现

    /* 函数要求:写一个函数模拟strstr()函数,设计中不得使用其他库函数. 函数原型:const char *strstr(const char *str1,const char *str2); ...

随机推荐

  1. RestTemplate对象的使用

  2. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  3. C++Primer 5th Chap10 Generic Algorithms(未完)

    大多数算法定义在头文件algorithm中,在头文件numeric中定义了数值泛型算法. 以find算法为例:在容器的两个迭代器指定的范围内遍历,查找特定值. auto result= cout< ...

  4. 1010 Radix:猥琐的测试数据

    谨以此题纪念边界测试数据浪费了我多少时间:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 # ...

  5. golang 管理 pidfile

    Pidfile 存储了进程的进程 id.一般情况下 pidfile 有以下几个作用: 其他进程可以读取 pidfile 获取运行进程的 pid(当然也可以通过其他命令 动态获取) 在启动进程前先检查 ...

  6. 去除echarts饼状图的引导线

    series: { name: "流量占比分布", type: "pie", radius: ["40%", "60%" ...

  7. (超实用)前端地址栏保存&获取参数,地址栏传输中文不在乱码

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://www.cnblogs.com/autoXingJY/p/115965 ...

  8. Luogu5307 [COCI2019] Mobitel 【数论分块】【递推】

    题目分析: 对于向上取整我们总有,$\lceil \frac{\lceil \frac{n}{a} \rceil}{b} \rceil = \lceil \frac{n}{a*b} \rceil$这个 ...

  9. Nginx 配置反向代理ip

    参考文档: https://blog.csdn.net/stevenprime/article/details/7918094

  10. Feign 客户端调用错误

    1.@RequestBody 必须要写在实现接口中 2.Feign 客户端调用的时候如果有参数的话,默认是发送post请求 3.服务接口中的请求参数必须要加上@RequestParam("r ...