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;
    }
};
相关文章
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
561 6
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
684 11
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
281 9
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
317 1
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
554 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
653 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
330 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
680 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
616 4
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
615 1

热门文章

最新文章