leetcode 28 找出字符串第一个匹配的下标(KMP实现strStr)

简介: leetcode 28 找出字符串第一个匹配的下标(KMP实现strStr)

实现strStr()


0715da47b35c433ab7b34f87e8d96a57.png

暴力法

class Solution {
public:
    int strStr(string haystack, string needle) {
        int i=0 , j=0;
        if (needle[0]=='\0' || haystack == needle) return 0;
        if(haystack.size() < needle.size()) return -1;
        while(i<= haystack.size()-needle.size())
        {
            // cout<<haystack[i]<<endl;
            if(haystack[i]==needle[0])
            {
               string temp = haystack.substr(i,needle.size());
            //    cout<<"temp "<<temp<<endl;
               if(temp == needle) return i;
            }
            i++;
        }
        return -1;
    }
};

KMP

#include <iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include <algorithm>
#include<map>
using namespace std;
class Solution {
public:
    void getNext(int* next, const string& s) //计算next数组
    {
        int j = -1;
        next[0] = j;
        for (int i = 1; i < s.size(); i++) 
        {
            while (j >= 0 && s[i] != s[j + 1])  //前后缀不同时后退,是while不是if,因为要连续后退不能只后退一次
            {
                j = next[j];
            }
            if (s[i] == s[j + 1]) //前后缀相同时,增加前后缀长度
            {
                j++;
            }
            next[i] = j; //i点时前后缀长度赋值
        }
    }
    int strStr(string haystack, string needle) 
    {
        if (needle.size() == 0) //如果模式组是长度为0,直接返回0
        {
            return 0;
        }
        int *next = new int[needle.size()]; //动态申请next数组,长度和模式串相同
        getNext(next, needle);
        int j = -1;
        for (int i = 0; i < haystack.size(); i++) //循环文本串
        {
            while (j>=0 && haystack[i] != needle[j+1]) //文本串和模式串不匹配,模式串回退
            {
                j = next[j];
            }
            if (haystack[i] == needle[j + 1]) //文本串和模式串匹配,模式串前进
            {
                j++;
            }
            if (j == needle.size() - 1) //文本串和模式串成功,前后缀公共长度为模式串-1
            {
              delete[] next;
                return (i - needle.size() + 1); //匹配成功字符串的头下标
            }
        }
    delete[] next;
        return -1;
    }
};
int main() {
    Solution a;
    string h = "sadbutsad";
    string n = "sad";
    int b = a.strStr(h, n);
    cout << b;
    return 0;
}

二刷

库函数

class Solution {
public:
    int strStr(string haystack, string needle) {
        return haystack.find(needle);
    }
};

暴力法

class Solution {
public:
    int strStr(string haystack, string needle) {
        for(int left=0 ; left < haystack.size() ;left++)
        {
            if(haystack[left] == needle[0])
            {
                for(int right = left ,i=0; right < haystack.size() && i<needle.size(); right++,i++)
                {
                    if(haystack[right] == needle[i])
                    {
                        if(i==needle.size()-1)
                        {
                            return left;
                        }
                        continue;
                    }
                    else break;
                }
            }
        }
        return -1;
    }
};

KMP

class Solution {
public:
    void getNext(int *next ,const string &s)
    {
        int j=-1;
        next[0] = j;
        for(int i=1 ; i<s.size();i++)
        {
            while( j>=0 && s[i] != s[j+1] )
                j = next[j];
            if(s[i] == s[j+1]) j++;
            next[i] = j;
        }
    }
    int strStr(string haystack, string needle) {
       if(needle.size() == 0 ) return 0;
       int next[needle.size()];
       int j=-1;
       getNext(next , needle);
       for(int i=0 ; i<haystack.size() ;i++)
       {
            while(j>=0 && haystack[i] != needle[j+1])
                j = next[j];
            if(haystack[i] == needle[j+1]) j++;
            if( j == needle.size() - 1 )
                return (i-needle.size()+1);
       }
       return -1;
    }
};
相关文章
|
4月前
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
50 1
|
4月前
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
38 9
|
4月前
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
36 0
|
4月前
【LeetCode 22】459.重复的子字符串
【LeetCode 22】459.重复的子字符串
47 0
|
4月前
【LeetCode 21】28. 实现 strStr()
【LeetCode 21】28. 实现 strStr()
43 0
|
4月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
34 0
|
5月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
6月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
75 6
|
6月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
145 2
|
3月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
98 1