实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

当 needle 是空字符串时我们应当返回 0 。

一开始的思路:用i和j从头开始分别遍历haystack和needle字符串,先固定j,直到i所指的字母与needle的首字符相等的时候,停止遍历。然后i和j开始同时向后遍历,如果有不相等的,就返回-1,直到有一个字符串先到头。若haystack先到头,则返回-1,若needle先到头或者同时到头,则返回i-needle.size()

这样的思路有一个问题,就是mississip和issip这种字符串,就是母串如果先出现一个类似子串但又不等于子串的部分,会造成干扰。

所以这里我们换个思路:我们在母串中,依次遍历子串长度的子串,看看能否找到和子串相等的部分,这样就不会漏掉上述的部分。

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

其实我们还可以利用string类中的现成的find函数

int strStr(string haystack, string needle)
{
string::size_type index = haystack.find(needle);
return index == string::npos ? -1 : index;
}

find函数只用了第一个参数,默认是从母串的开头查找,找到和needle相同的子串就停止,返回位置,若找不到,则返回string::npos。

Leetcode(28)-实现strStr()的更多相关文章

  1. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  2. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  3. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  4. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  5. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  7. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  8. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  9. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  10. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. 深入研究.NET 5的开放式遥测

    OpenTelemetry 介绍 OpenTelemetry是一种开放的源代码规范,工具和SDK,用于检测,生成,收集和导出遥测数据(指标,日志和跟踪),开放遥测技术得到了Cloud Native C ...

  2. Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之管理 Transactions

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  3. 被集群节点负载不均所困扰?TKE 重磅推出全链路调度解决方案

    引言 在 K8s 集群运营过程中,常常会被节点 CPU 和内存的高使用率所困扰,既影响了节点上 Pod 的稳定运行,也会增加节点故障的几率.为了应对集群节点高负载的问题,平衡各个节点之间的资源使用率, ...

  4. HTTP协议相关知识整理:

    http协议简介 超文本传输协议:是一种用于分布式.协作式和超媒体信息系统的应用层协议. 一次请求一次响应之后断开连接(无状态,短连接) 分析http请求信息格式 http工作原理 以下是 HTTP ...

  5. jQuery 实现复制功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. JAVA SSM整合流程以及注意点

    1.搭建整合环境 整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式 先搭建整合的环境 先把Spring的配置搭建完成 再使用Spring整合SpringMVC框架 最后使用Spr ...

  7. OPTIONS的预请求(Preflighted Request)

    OPTIONS的预请求(Preflighted Request) Ajax 请求中出现OPTIONS(Request Method: OPTIONS)_qiao-CSDN博客 https://blog ...

  8. ElasticSearch基本简介(一)

    一.ES简介 1,什么是ES ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式的全文搜索引擎,其对外服务是基于RESTful web接口发布的.Elasticsearc ...

  9. tp6.0配置站点后无法访问

    1.安装composer中国镜像composer config -g repo.packagist composer https://packagist.phpcomposer.com2.执行comp ...

  10. React-Router browserHistory浏览器刷新出现页面404解决方案

    在React项目中我们经常需要采用React-Router来配置我们的页面路由,React-Router 是建立在 history 之上的,常见的history路由方案有三种形式,分别是: 1.has ...