/*
* @lc app=leetcode.cn id=28 lang=c
*
* [28] 实现strStr()
*
* https://leetcode-cn.com/problems/implement-strstr/description/
*
* algorithms
* Easy (37.86%)
* Total Accepted: 38.6K
* Total Submissions: 102K
* Testcase Example: '"hello"\n"ll"'
*
* 实现 strStr() 函数。
*
* 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置
* (从0开始)。如果不存在,则返回  -1。
*
* 示例 1:
*
* 输入: haystack = "hello", needle = "ll"
* 输出: 2
*
*
* 示例 2:
*
* 输入: haystack = "aaaaa", needle = "bba"
* 输出: -1
*
*
* 说明:
*
* 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
*
* 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
*
*/
int strStr(char* haystack, char* needle) { int i, j; int len1 = strlen(haystack); int len2 = strlen(needle); for(i = ; i <= len1 - len2; i++){ for(j = ; j < len2; j++){ if(haystack[i + j] != needle[j]){ break; } } if(j == len2) return i; } return -; }

c语言自然是应用最最著名的kmp(看毛片)算法。

这个算法的理解可以参考:

https://www.cnblogs.com/yjiyjige/p/3263858.html

--------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=28 lang=python3
#
# [28] 实现strStr()
#
# https://leetcode-cn.com/problems/implement-strstr/description/
#
# algorithms
# Easy (37.86%)
# Total Accepted: 38.6K
# Total Submissions: 102K
# Testcase Example: '"hello"\n"ll"'
#
# 实现 strStr() 函数。
#
# 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置
# (从0开始)。如果不存在,则返回  -1。
#
# 示例 1:
#
# 输入: haystack = "hello", needle = "ll"
# 输出: 2
#
#
# 示例 2:
#
# 输入: haystack = "aaaaa", needle = "bba"
# 输出: -1
#
#
# 说明:
#
# 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
#
# 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
#
#
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
l = len(needle)
for i in range(len(haystack)-l+1):
if haystack[i:i+l] == needle:
return i
return -1

python是真的简单,运用切片就能达到想要的目的了。

Leecode刷题之旅-C语言/python-28.实现strstr()的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  4. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  5. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  6. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  7. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  8. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  9. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. BIEE入门(四)展现层

    BIEE里最终面向最终用户(业务界面使用者的)叫做BIEE的Presentation Layer也即展现层,展现层的定义将是最终用户Web报表开发界面里能够看见的完全一样的样子,所以展现层一般将是以最 ...

  2. SQL 数据匹配更新

    萌新的成长之路! 最近遇到的一个需求是需要将两张关联的表的数据进行匹配(dbo.SystemUser[系统用户表]:dbo.V_Temp[系统权限视图]),即通过匹配用户ID与系统ID(拥有的系统ID ...

  3. web端 调试 手机混合应用中的h5部分(chrome浏览器的devtool使用)

    Learning Hybird App Test–Appium Java(Leyden) 浏览器的远程调试工具,使得我们可以通过PC上开启的控制台,调试手机浏览器中正在运行的代码.运行于 Androi ...

  4. harbor使用aws s3存储

    参考:http://www.vmtocloud.com/how-to-configure-harbor-registry-to-use-amazon-s3-storage/ s3 bucket权限:更 ...

  5. scrum3

    首先我一直做的是框架的设计,但不同的是这次我们整合完善了这个软件目前的所有需求也定义好了它的大题框架,总的来说设计部分已经结束,现在也就是本次冲刺,我们将重点进行整个软件的数据库编程环节,也就是用SQ ...

  6. 初识Git与Github

    学习和使用Git和Github的确是一件很有意义的事,通过使用Git和Github,可以让我们很方便地管理自己的各种文件,还可以帮助一名程序员更好地用于代码管理.而对于一名软件技术人员,建立自己的Gi ...

  7. 找出OData service出错根源的小技巧

    SAP的Fiori应用是通过OData和后台交互的.在使用Fiori应用时您可能会遇到这样的错误消息: 这个错误消息没有包含有助于partner或者客户定位问题根源的线索. 下面是如何在后台找出问题根 ...

  8. python 带BOM头utf-8的响应解码

    接口响应编码格式为带BOM头utf-8.直接获取响应的text出现乱码. '''dinghanhua2018-11requests text与content,指定响应的encoding''' api ...

  9. HDU 4944 逆序数对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 题意: 给出一个序列,可以相邻的交换k次,求 k 次之后,逆序数对最少是多少: 分析: 可以发现 ...

  10. 【luogu P4568 [JLOI2011]飞行路线】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4568 卡了一晚上,算是分层图最短路的模板.注意卡SPFA,所以我写了个SLF优化. 同时 AC400祭!~ ...