• 面试经典 150 题 22 —(数组 / 字符串)— 28. 找出字符串中第一个匹配项的下标


    28. 找出字符串中第一个匹配项的下标

    在这里插入图片描述

    方法一
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            if(haystack.find(needle) == string::npos){
                return -1;
            }
            return haystack.find(needle);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    方法二
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int haystackLength = haystack.length();
            int needleLength = needle.length();
    
            int haystackIndex = 0, needleIndex = 0;
            while(haystackIndex < haystackLength){
                
                if(haystack[haystackIndex] != needle[needleIndex]){
                    // 如果不相等,haystack从最开始匹配相等的地方重新进行
                    haystackIndex = haystackIndex - needleIndex; 
                    // needle从头开始
                    needleIndex = 0;
                }
                else{
                   
                    if(needleIndex == needleLength-1){
                        return haystackIndex-needleLength+1;
                    }
                    needleIndex++;
                }
                haystackIndex++;
            }
            return -1;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int haystackLength = haystack.size(),needleLength = needle.size();
            if(needleLength == 0){
                return 0;
            }
    
            // KMP算法:如果已经匹配的字符串包含相同的前缀和后缀,遇到下一个不匹配的位置时,指向needle的指针跳转到前缀的后一个位置,还是不匹配的话,再往前跳转后继续比较;先构造一个next数组来记录needle指针跳转的位置
            // 先构造next数组,next数组中的元素表示当前两个元素不匹配时,needle指针要跳转的位置
            // haystack: [a, b, e, a, b, a, b, e, a, b, f]
            // needle:   [a, b, e, a, b, f]
            // next:     [0, 0, 0, 1, 2, 0]
            vector<int> next(needleLength);
            for(int i=1,j=0; i<needleLength; i++){
                while(j>0 && needle[i]!=needle[j]) {
                    j = next[j-1]; // 一直和前一位置的值比较,直到遇到相等的字符或者j=0;j通过next[j-1]来回退
                }
                if(needle[i]==needle[j]){
                    j++;
                };
                next[i] = j;
            }
            // 利用next数组进行跳转匹配,不再需要回退haystack的指针i
            for(int i=0,j=0; i<haystackLength; i++){
                // 匹配不成功,needle指针j回退并继续比较
                while(j>0 && haystack[i]!=needle[j]){
                    j = next[j-1];
                }
                if(haystack[i]==needle[j]){
                    j++;
                }
                if(j==needleLength){
                    return i - needleLength + 1;
                }
            }
            return -1;
    
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    debian11 安装后必备配置
    【深度学习】YOLOv5 工程落地部署过程 MNN
    黑马点评回顾 redis实现共享session
    springboot+英语在线学习系统 毕业设计-附源码211714
    JAVA要点
    Goby 漏洞发布|用友 U8 Cloud ActionHandlerServlet 远程代码执行漏洞
    一文详解如何用 MySQL/Redis/ZooKeeper 实现分布式锁
    Python教程(14)——Python函数的入门学习
    每天一道算法题:216. 组合总和 III
    Day19—Scrapy框架高级特性
  • 原文地址:https://blog.csdn.net/weixin_44032178/article/details/133742472