/*
* @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. 操作Active Directory C#

    .Net平台操作活动目录Active Directory,使用System.DirectoryServices.ActiveDirectory,主要是User OU 和Group的操作. 代码运行了一 ...

  2. MySQL的基础(优化)2

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  3. 在centos7中安装redis,并通过node.js操作redis

    引言 最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis.所以顺便做个笔记. 如有不对的地方,欢迎大家 ...

  4. Linux --远程访问控制

    1.SSH服务端 修改配置文件 vi /etc/ssh/sshd_config 监听选项 port 22 //监听端口地址 ListenAddress 192.168.1.50 //监听地址为本机地址 ...

  5. 《3D打印:从想象到现实》:基本没发现独到之处

    本书汇总了3D打印的相关咨询:原理.可能的或已经实现的应用.商业模式等等.由于3D打印是最近媒体上比较热的信息,对我来说书中的大部分内容都没有独到之处,都是已知的.

  6. kahadb设计

    Kahadb设计思想   简介 hakadb是activemq的持久化数据库,作为消息队列的存储,每个消息有一个消息ID,提供了对消息的快速的查找,更新,以及消息的事物支持,以及意外磬机之后的恢复.丰 ...

  7. 抓取android系统日志_记录一次定位app闪退故障

    在测试android客户端兼容性时,发现app闪退,上海的小伙伴需要闪退时的系统日志:故把快生锈的adb知识拿出来show一把: 1.下载adb工具包(adb的全称为Android Debug Bri ...

  8. QT学习之窗口右键菜单

    #QT学习之窗口右键菜单 QWidget 及其子类都有右键菜单,主要因为其有两个与右键菜单相关联的函数: Qt::ContextMenuPolicy contextMenuPlicy() const ...

  9. note01-计算机网络

    1.基础概述 三网: 电信网络.有线电视网络.计算机网络(连通&共享) 终端->z本地ISP->x地区IXP->y主干ISP 通信方式: C/S .P2P(即是client也 ...

  10. java GZIP 压缩数据

    package com.cjonline.foundation.cpe.action; import java.io.ByteArrayInputStream; import java.io.Byte ...