博客
关于我
leetcode-28-Implement strStr()
阅读量:809 次
发布时间:2023-01-31

本文共 1446 字,大约阅读时间需要 4 分钟。

为了解决这个问题,我们需要编写一个函数来查找 haystack 中首次出现的 needle 子字符串的索引,如果 needle 不在 haystack 中,则返回 -1。

方法思路

为了高效地解决这个问题,我们可以采用以下步骤:

  • 处理特殊情况

    • 如果 needle 为空字符串,返回 0,因为空字符串可以被认为是任何字符串的子字符串。
    • 如果 haystack 为空字符串,而 needle 不为空,返回 -1,因为空字符串无法包含非空子字符串。
    • 如果 needle 和 haystack 都为空字符串,返回 0。
  • 检查长度

    • 如果 haystack 的长度小于 needle 的长度,返回 -1,因为 needle 无法出现在 haystack 中。
  • 查找子字符串

    • 遍历 haystack,从第一个字符开始,直到 haystack 可以包含 needle 的最后一个位置。
    • 使用 substr 方法提取子字符串进行比较,如果找到匹配项,返回当前索引;否则,遍历结束后返回 -1。
  • 这种方法的时间复杂度为 O(n*m),其中 n 是 haystack 的长度,m 是 needle 的长度。在大多数情况下,这是一个可接受的时间复杂度,适用于一般的字符串查找问题。

    解决代码

    #include 
    using namespace std;int strStr(string haystack, string needle) { // 如果 needle 为空,返回 0 if (needle.empty()) { return 0; } // 如果 haystack 为空,则不能包含 needle,返回 -1 if (haystack.empty()) { return -1; } // 检查 haystack 的长度是否大于等于 needle 的长度 int haystackLength = haystack.length(); int needleLength = needle.length(); if (haystackLength < needleLength) { return -1; } // 遍历 haystack, 从开始到 haystackLength - needleLength 的位置 int maxIndex = haystackLength - needleLength; for (int i = 0; i <= maxIndex; ++i) { string sub = haystack.substr(i, needleLength); if (sub == needle) { return i; } } // 如果遍历完都没找到,返回 -1 return -1;}

    代码解释

    • 处理特殊情况:检查针是否为空字符串,根据情况返回相应的值。
    • 长度检查:如果 haystack 的长度小于 needle 的长度,直接返回 -1。
    • 遍历查找:从 haystack 的起始位置开始,逐步提取子字符串进行比较,找到匹配项后返回当前索引。遍历结束后如果没有找到,返回 -1。

    这种方法确保了代码的正确性和高效性,同时也处理了所有边界情况。

    转载地址:http://algyk.baihongyu.com/

    你可能感兴趣的文章
    ssm旅游信息管理系统的设计与实现bus56(程序+开题)
    查看>>
    order by rand()
    查看>>
    SSM(Spring+SpringMvc+Mybatis)整合开发笔记
    查看>>
    ViewHolder的改进写法
    查看>>
    Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
    查看>>
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>