首页 > 其他分享 >【LeetCode】28. 找出字符串中第一个匹配项的下标 -- 459. 重复的子字符串

【LeetCode】28. 找出字符串中第一个匹配项的下标 -- 459. 重复的子字符串

时间:2023-02-21 20:57:44浏览次数:51  
标签:459 return -- needle next int 字符串 size

  1. 找出字符串中第一个匹配项的下标
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0) return 0;
        vector<int> next(needle.size());
        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)) {
                return (i - needle.size() + 1);
            }
        }
        return -1;
    }
    void getNext(vector<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;
        }
    }
};
  1. 重复的子字符串
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        return (s + s).find(s, 1) != s.size();
    }
};

标签:459,return,--,needle,next,int,字符串,size
From: https://www.cnblogs.com/zjacky/p/17142317.html

相关文章

  • 智能文本自动处理(Intelligent text automatic processing)(二)
    AutoText智能文本自动处理工具(Intelligenttextautomaticprocessingtool)。项目地址:https://github.com/jiangnanboy/AutoTextAutoText的功能主要有文本纠错,图片ocr以......
  • 安装gitlab
    1.配置yum源vim/etc/yum.repos.d/gitlab-ce.repo内容[gitlab-ce]name=GitlabCERepositorybaseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gpg......
  • selenium之关闭窗口,指定窗口大小,前进,后退,刷新等操作
    关闭窗口1、仅关闭当前窗口(Tab页),其他窗口不退出关闭用户当前正在使用的Web浏览器窗口,即WebDriver当前正在访问的窗口。.close()方法既不需要任何参数,也无任何返回值。......
  • 快速入门
    管理系统数据库设计时,  设计的实体关系图EntityRelationshipDiagram(包含了实体,关系,属性)一般来描述数据库表的关系     powerdesigner的使用:学习的......
  • Rocky Linux安装
    1.下载VirtualBox并安装https://www.virtualbox.org/wiki/Downloads2.下载RockyLinux选择Rocky-9.1-x86_64-dvd.iso镜像官网https://rockylinux.org/download......
  • 每日总结 2/21
     在2月21日,我完成了androidstudio的下载和安装, 在安装androidstudio的时候我的电脑总是出现一些问题,最开始下载的是2022版本的,但是最后下载了2021版本的就能用了,并......
  • matplotlib 散点图
    应用场景:探究不同变量之间的内在关系importmatplotlibimportmatplotlib.pyplotaspltimportnumpyasnpif__name__=="__main__":#0、修改支持中文的字......
  • 常用容器
    List链表,是一个接口;初始化List<Integer>list=newArrayList<>;//长数组,支持下标随时访问,访问的时间复杂度是O(1);List<Integer>list=newLinkList<>;//双链表,访......
  • 2.21博客园
    博客园2月21日今天我在昨天基础上,继续写了add的数据jsp和显示jsp完成了老师昨天的任务,实现了项目的增和浏览功能。另外我还在今天杨子光老师的数据库课上学到了很多专业知......
  • MySQL事务
    MySQL事务概念什么是事务?事务是用于保证数据一致性,它由一组相关的dml语句组成,改组的dml语句,要么全部成功,要么全部失败。事务和锁当执行事务操作时,MySQL会在表上加......