【Leetcode】Implement strStr()

基于pytest+Allure+Python+CANoe的汽车诊断自动化测试框架搭建实战 自动化测试是提升软件质量和研发效率的关键技术,其核心原理是通过脚本模拟人工操作,实现测试用例的自动执行与结果验证。在汽车电子领域,诊断服务测试是确保车辆ECU(电子控制单元)功能正常的重要环节,传统手动测试在面对海量用例和频繁回归时效率低下。本文聚焦于如何利用Python的pytest测试框架、Allure报告工具以及Excel数据驱动,构建一个与Vector CANoe仿真测试环境深度集成的自动化测试解决方案。该方案通过Python调用CANoe的COM接口,实现了诊断报文的自动发送与响应验证,并结合Al 阅读详情

题目:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

翻译: 

实现一个方法strStr()。返回字符串needle第一次在字符串haystack出现的下标,如果needle不是haystack的一部分,就返回-1。


分析:

在文本中查找某个模式出现的位置的算法,称为字符串匹配算法。常用的方法有朴素字符串匹配算法、KMP算法等。朴素字符串匹配算法,就是把2个字符串头部对齐,然后逐一字符匹配,失配后,把needle右移一位,继续从头匹配。


Java版本:

class Solution {
    public int strStr(String haystack, String needle) {
    /**brute force的算法,假设原串的长度是n,匹配串的长度是m。思路很简单,
    就是对原串的每一个长度为m的字串都判断是否跟匹配串一致。总共有n-m+1个子串,
    所以算法时间复杂度为O((n-m+1)*m)=O(n*m),空间复杂度是O(1)。**/
    //     if(haystack==null || needle == null || needle.length()==0)  
    //         return 0;  
    //     if(needle.length()>haystack.length())  
    //         return -1;  
    //     for(int i=0;i<=haystack.length()-needle.length();i++)  
    //     {  
    //         boolean successFlag = true;  
    //         for(int j=0;j<needle.length();j++)  
    //         {  
    //             if(haystack.charAt(i+j)!=needle.charAt(j))  
    //             {  
    //                 successFlag = false;  
    //                 break;  
    //             }  
    //         }  
    //         if(successFlag)  
    //             return i;  
    //     }  
    //     return -1;  
    // }
        
        // 另一种简洁算法:
        int l1 = haystack.length(), l2 = needle.length();
        if (l1 < l2) {
            return -1;
        } else if (l2 == 0) {
            return 0;
        }
        int threshold = l1 - l2;
        for (int i = 0; i <= threshold; ++i) {
            if (haystack.substring(i,i+l2).equals(needle)) {
                return i;
            }
        }
        return -1;
    }
}
注意的是,从haystack第一个元素下标0开始,界限为两个数组的差值threshold,从haystack第一个元素+needle的长度与needle进行判断比较,类似个方块形状来进行块状的匹配一样。

Python版本:

class Solution(object):
def strStr(self, haystack, needle):
    """
    :type haystack: str
    :type needle: str
    :rtype: int
    """
    for i in range(len(haystack) - len(needle)+1):
        if haystack[i:i+len(needle)] == needle:
            return i
    return -1


C++版本:

class Solution {
public: 
    int strStr(string haystack, string needle) {
        int m = haystack.length(), n = needle.length();
        if (!n) return 0;
        for (int i = 0; i < m - n + 1; i++) {
            int j = 0;
            for (; j < n; j++)
                if (haystack[i + j] != needle[j])
                    break;
            if (j == n) return i;
        }
        return -1;
    }
};




从入门到精通:dirmap 高级 Web 目录扫描实战指南 本文提供了一份从环境搭建到高级配置的dirmap Web目录扫描实战指南。详细解析了工具安装、核心配置文件dirmap.conf的深度调优,包括递归扫描、四种扫描模式(字典、爆破、爬虫、Fuzz)的实战应用,并演示了分阶段资产发现的完整工作流程,帮助安全人员高效发现隐藏目录与敏感文件。 阅读详情

相关推荐

成本会计(第九版)王忠孝课后习题答案解析

在掌握了基础知识之后,我们还需要培养自己的实际操作能力。此外,在学习过程中,遇到困难时,要及时向老师、同学请教,不要害怕暴露自己的不足。三人行,必有我师,每个人都有值得学习的地方。首先,我们需要认真阅读教材,把基础知识掌握牢固。在这里,我建议大家使用《成本会计(第九版)》的配套练习册,这样可以帮助我们检查学习效果。同时,网上的一些优质课程和论坛也是不错的学习资源,例如网易云课堂、中国大学MOOC等。别担心,这里我给大家推荐一些实用的学习资料和方法,帮助你更好地掌握这门课程。出版社: 大连理工大学出版社。

weixin_58833001的博客 1888

【leetcode】28. 实现 strStr()(Implement strStr())

题目描述 【leetcode】28. 实现 strStr()(Implement strStr()) 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。 第一次解答 思路: 感觉与 最长公共前缀 类似 感觉与 剔除有序数组重复元素类似...

Liber-coder的博客 272

利用WebDAV协议将百度网盘挂载为本地网络硬盘的完整指南

WebDAV是一种基于HTTP/HTTPS的扩展协议,它允许客户端通过标准HTTP方法对远程服务器上的文件进行读写和管理。其核心原理是通过定义如PROPFIND、PUT、GET等标准化方法,实现跨平台的远程文件系统访问。这项技术的价值在于打破了不同存储服务间的壁垒,将封闭的云存储生态接入开放标准协议体系。在工程实践中,WebDAV常被用于实现跨设备文件同步、媒体服务器集成和生产力工具数据备份等场景。通过搭建WebDAV服务器作为协议转换层,可以将百度网盘这类封闭存储服务映射为标准网络存储,实现类似bdpan

weixin_42523907的博客 2427

[LeetCode][Java] Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 题意: 实现strStr() 返回needle在haystack中第一次出现的位置,如果haystack中不存在needl

西湖有大葱的专栏 1833

STM32 IAP跳转后APP卡死?别慌,手把手教你修复HAL_RCC_OscConfig时钟配置冲突

本文深入分析了STM32 IAP跳转后APP在HAL_RCC_OscConfig函数中卡死的问题,揭示了PLL时钟配置冲突的根本原因。通过详细的代码示例和分步解决方案,指导开发者正确处理STM32F1/F4系列的时钟系统二次初始化问题,确保IAP+APP架构的稳定运行。

weixin_28685287的博客 365

MySQL安装最后启动报错--Job for mysqld.service failed because the control process exited with error code. See

MySQL安装最后启动报错: [root@localhost mysql]# systemctl start mysqld Warning: mysqld.service changed on disk. Run 'systemctl daemon-reload' to reload units. Job for mysqld.service failed because the control process exited with error code. See "systemctl status m

chenchen的博客 2746

leetcode-028. Implement strStr()

题目要求匹配给定字符串和模式串,如果模式串存在于字符串中,则返回所在索引,如果不存在,则返回-1; 关于给定字符串和模式串的匹配问题,是数据结构的经典问题,也有其对应的解决算法,即KMP算法。然而数据结构中引出KMP算法也是为了解决暴力求解时,两个指针回溯问题,导致o(mn)的时间复杂度而提出的。 因此对于字符串的匹配,我们首先想到的就是暴力求解法。(*/ω\*) 一、暴力求解 思路:...

orangefly0214的博客 312

GeoServer改造Springboot源码一(公共部分)

今天开始开启关于GeoServer的一个全新系列,主要是把改造Springboot后的详细代码粘贴出来,此文应配合《》系列共同阅读,按照前系列的时间顺序结合此系列的源码展示可以快速构建GeoServer功能的二次封装的后端系统。

龙井的专栏 1902

LeetCode-Implement strStr()

作者:disappearedgod 文章出处:http://blog.youkuaiyun.com/disappearedgod/article/details/23621903 时间:2014-8-22

上帝已逝,自求多福 932

华为OD机试真题 新系统2026-04-08 C++ 实现【直捣黄龙】

小王在玩一款叫做直捣黄龙的小游戏,在该游戏中他需要从入口位置进入敌营,绕过哨兵的层层封锁,达到敌军司令部实施斩首行动。 敌军阵营是一个n*n的矩阵,入口在坐标(0,n/2),敌军司令部在坐标(n-1,n/2),每个 哨兵警戒以自己为中心的9宫格,一旦被哨兵发现则行动失败。

MISAYAONE的博客 261

【用unity实现100个游戏之16】Unity程序化生成随机2D地牢游戏1(附项目源码)

关于使用TileMap生成随机2D地图,其实之前已经有做过类似的,感兴趣可以看看 但是随着学习深入,发现之前做的比较粗糙和不够全面,最近又在外网看到

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2215

LeetCode:Implement strStr()

Implement strStr() Total Accepted: 109580 Total Submissions: 435297 Difficulty: Easy Implement strStr(). Returns the index of the first occurrence of needle in haystack, or

walker lee 2146

VMware16虚拟机:安装Windows10系统---超详细教程

VMware16虚拟机:安装Windows10系统---超详细教程

MQ0522的博客 3万+

【力扣Leetcode题解系列之0028— Implement strStr() 实现 strStr()】

摘要 LeetCode 28题要求实现字符串匹配函数strStr(),即在haystack中查找needle首次出现的位置,未找到则返回-1。本文提供了Python、Java、C、C++和Go的暴力匹配解法,均通过遍历haystack并逐字符比较实现。算法时间复杂度为O((M-N+1)*N),空间复杂度O(1)。关键点包括边界条件处理(如空needle返回0)和子串比较逻辑。扩展方向可涉及多位置匹配、模糊匹配或动态字符串场景。该问题在文本搜索、生物基因分析等领域有实际应用。

youngerwang的博客 1237

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode力扣 28. 实现 strStr() Implement strStr() 题解代码 JavaScript

问题https://leetcode-cn.com/problems/implement-strstr/ 练习使用JavaScript解答 /** * @param {string} haystack * @param {string} needle * @return {number} */ var strStr = function(haystack, needle) { return haystack.indexOf(needle); }; 题目是让实现,直接用库好像不太合适.

Coding home - 漂流瓶jz 514

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

LeetCode力扣之Implement strStr()

Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll" Output: 2 Example 2:Input:...

LeeWei 237

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

LeetCode 之 Implement strStr() — C 实现

Implement strStr()   Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 实现 strStr(). 返回字串在字符串中第一次出现的位置,不存在则返回-1.

~心有所依~ 的博客 448

LeetCode 28. Implement strStr()

LeetCode 28. Implement strStr() (Easy) 主要知识点:字符串、KMP算法;优先级:2

温迪今天刷题了吗 227

Leetcode 28. Implement strStr() 实现 strStr()

@[TOC](Leetcode 28. Implement strStr() 实现 strStr()) 28. Implement strStr() 题目描述 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of hayst...

importsys的博客 291

leetcode 28. Implement strStr() KMP 算法

leetcode 28. Implement strStr() 要理解next数组的构造思想,学习博客  http://www.cnblogs.com/yjiyjige/p/3263858.html。 public class Solution { public int[] getNext(String t){ int tlen = t.length();

独钓寒江雪 495

Leetcode 28. Implement strStr()

class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ return haystack.find(...

weixin_30662109的博客 83

[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. Update (2014-11-02): The signature of the

不积跬步无以至千里 629

LeetCode OJ 28 Implement strStr() [Easy]

LeetCode OJ 28  Implement strStr()   题目描述: Implement strStr(). Returns the index of the first occurrenceof needle in haystack, or -1 if needle is not part of haystack. 题目理解: 给定两个字符串haystack和ne

大吱的博客 284

Implement strStr() [LeetCode] + KMP

Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 实现了下,KMP算法明显速度提高y

_Legolas_的技术专栏 1082

LeetCode算法入门- Implement strStr() -day22

LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack ...

hongjie_lin 218

LeetCode:Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 题目的要求是寻找...

remanented的博客 198
上一篇: 【Leetcode】Merge Two Sorted Lists
下一篇: 【Leetcode】Merge Intervals
「已注销」
博客等级 码龄10年 51粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值