广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++实现LeetCode(28.实现strStr()函数)
  • 534
分享到

C++实现LeetCode(28.实现strStr()函数)

2024-04-02 19:04:59 534人浏览 薄情痞子
摘要

[LeetCode] 28. Implement strStr() 实现strStr()函数 Implement strStr(). Return the index of

[LeetCode] 28. Implement strStr() 实现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: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回 -1。然后开始遍历母字符串,这里并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可,代码如下:


class Solution {
public:
    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;
    }
};

我们也可以写的更加简洁一些,开头直接套两个 for 循环,不写终止条件,然后判断假如j到达 needle 的末尾了,此时返回i;若此时 i+j 到达 haystack 的长度了,返回 -1;否则若当前对应的字符不匹配,直接跳出当前循环,参见代码如下:

解法二:


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

到此这篇关于c++实现LeetCode(28.实现strStr()函数)的文章就介绍到这了,更多相关C++实现实现strStr()函数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++实现LeetCode(28.实现strStr()函数)

本文链接: https://www.lsjlt.com/news/130431.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++实现LeetCode(28.实现strStr()函数)
    [LeetCode] 28. Implement strStr() 实现strStr()函数 Implement strStr(). Return the index of...
    99+
    2022-11-12
  • C++怎么实现strStr()函数
    本文小编为大家详细介绍“C++怎么实现strStr()函数”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++怎么实现strStr()函数”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Implement str...
    99+
    2023-06-19
  • C语言模拟实现strstr函数的示例代码
    目录strstr函数介绍BF算法介绍BF算法模拟实现strstr函数KMP算法介绍KMP算法模拟实现strstr函数strstr函数介绍 C语言提供了字符串匹配函数 strstr 函...
    99+
    2022-11-13
  • C语言模拟实现strstr函数的代码怎么写
    这篇文章主要介绍了C语言模拟实现strstr函数的代码怎么写的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C语言模拟实现strstr函数的代码怎么写文章都会有所收获,下面我们一起来看看吧。strstr函数介绍C...
    99+
    2023-07-02
  • C语言strlen,strcpy,strcmp,strcat,strstr字符串操作函数实现
    目录strlenstrcpystrcmpstrcatstrstr前言: 今天来实现strlen、strcpy、strcmp、strcat、strstr三个比较常见的字符串操作函数,具...
    99+
    2022-11-13
  • C++实现LeetCode(202.快乐数)
    [LeetCode] 202.Happy Number 快乐数 Write an algorithm to determine if a number is "happy". A h...
    99+
    2022-11-12
  • C++实现LeetCode(169.求大多数)
    [LeetCode] 169. Majority Element 求大多数 Given an array nums of size n, return&...
    99+
    2022-11-12
  • C++实现LeetCode(15.三数之和)
    [LeetCode] 15. 3Sum 三数之和 Given an array S of n integers, are there elem...
    99+
    2022-11-12
  • C++实现LeetCode(7.翻转整数)
    [LeetCode] 7. Reverse Integer 翻转整数 Given a 32-bit signed integer, reverse digits of an inte...
    99+
    2022-11-12
  • C++实现LeetCode(65.验证数字)
    [LeetCode] 65.Valid Number 验证数字 Validate if a given string can be interpreted as a dec...
    99+
    2022-11-12
  • C++实现LeetCode(18.四数之和)
    [LeetCode] 18. 4Sum 四数之和 Given an array S of n integers, are there elements a, b, c, and d ...
    99+
    2022-11-12
  • C++实现LeetCode(29.两数相除)
    [LeetCode] 29. Divide Two Integers 两数相除 Given two integers dividend and divi...
    99+
    2022-11-12
  • C++实现LeetCode(36.验证数独)
    [LeetCode] 36. Valid Sudoku 验证数独 Determine if a 9x9 Sudoku board is valid. O...
    99+
    2022-11-12
  • C++实现LeetCode(37.求解数独)
    [LeetCode] 37. Sudoku Solver 求解数独 Write a program to solve a Sudoku puzzle by filling the e...
    99+
    2022-11-12
  • C++实现LeetCode(189.旋转数组)
    [LeetCode] 189. Rotate Array 旋转数组 Given an array, rotate the array to the right by k&#...
    99+
    2022-11-12
  • C++实现LeetCode数组练习题
    目录1、存在重复元素2、最大子序和3、两数之和4、合并两个有序数组5、两个数组的交集II6、买卖股票的最佳时机7、杨辉三角8、重塑矩阵9、有效的数独10、矩阵置零总结1、存在重复元素...
    99+
    2022-11-12
  • C++如何实现LeetCode
    这篇文章给大家分享的是有关C++如何实现LeetCode的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。[LeetCode] Reverse Linked List 倒置链表Reverse a singly lin...
    99+
    2023-06-20
  • C++怎么实现LeetCode
    这篇文章主要介绍“C++怎么实现LeetCode”,在日常操作中,相信很多人在C++怎么实现LeetCode问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么实现LeetCode”的疑惑有所帮助!接下来...
    99+
    2023-06-20
  • C++实现LeetCode(200.岛屿的数量)
    [LeetCode] 200. Number of Islands 岛屿的数量 Given a 2d grid map of '1's (land) and '0...
    99+
    2022-11-12
  • C++实现LeetCode(149.共线点个数)
    [LeetCode] 149. Max Points on a Line 共线点个数 Given n points on a 2D plane, find the...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作