【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr()
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
解法一:暴力解
class Solution {
public:
int strStr(string haystack, string needle) {
int m = haystack.size();
int n = needle.size();
for(int i = ; i <= m-n; i ++)
{
int j;
for(j = ; j < n; j ++)
{
if(haystack[i+j] != needle[j])
break;
}
if(j == n)
return i;
}
return -;
}
};
解法二:标准KMP算法。可参考下文。
(1)先对模式串needle做“自匹配”,即求出模式串前缀与后缀中重复部分,将重复信息保存在next数组中。
(2)依据next数组信息,进行haystack与needle的匹配。
class Solution {
public:
int strStr(char *haystack, char *needle) {
int hlen = strlen(haystack);
int nlen = strlen(needle);
int* next = new int[nlen];
getNext(needle, next);
int i = ;
int j = ;
while(i < hlen && j < nlen)
{
if(j == - || haystack[i] == needle[j])
{// match current position, go next
i ++;
j ++;
}
else
{// jump to the previous position to try matching
j = next[j];
}
}
if(j == nlen)
// all match
return i-nlen;
else
return -;
}
void getNext(char *needle, int next[])
{// self match to contruct next array
int nlen = strlen(needle);
int j = -; // slow pointer
int i = ; // fast pointer
next[i] = -; //init next has one element
while(i < nlen-)
{
if(j == - || needle[i] == needle[j])
{
j ++;
i ++; //thus the condition (i < nlen-1)
next[i] = j; //if position i not match, jump to position j
}
else
{
j = next[j]; //jump to the previous position to try matching
}
}
}
};
【LeetCode】28. Implement strStr() (2 solutions)的更多相关文章
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】28. 实现 strStr()
28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
随机推荐
- centos6.5安装部署zabbix监控服务端和客户端
部署zabbix服务端需要LNMP环境(nginx,mysql,php),其它数据库也可以,我这里使用mysql,关于LNMP环境部署,可以参考我的另一遍文章:http://www.cnblogs.c ...
- 尚硅谷面试第一季-12Linux常用服务类相关命令
课堂重点: 实操命令及运行结果: (centos 6) service network status chkconfig --list chkconfig --level 5 network off ...
- phpMyAdmin - Error
centos7.5 原因:权限问题 解决方法:授权 [root@web01 code]# groupadd -g666 www [root@web01 code]# useradd -u666 -g6 ...
- python --- 03 整型 bool 字符串 for循环
一.整型(int) 基本操作: 1.+ - * / % // ** 2. .bit_length() 计算整数在内存中占⽤的⼆进制码的⻓度 如: 二.布尔值(bool) True False 1. ...
- Linux lvm 分区知识笔记
盘面上可以细分出扇区(Sector)与柱面(Cylinder)两种单位,其中扇区每个为512bytes那么大. 通常所说的"硬盘分区"就是指修改磁盘分区表,它定义了"第n ...
- CodeChef - MRO Method Resolution Order(打表)
题意:有一种关系叫继承,那么继承父类的同时也会继承他的一个函数f,能继承任意多个父类或不继承,但不能继承自己的子类.现在规定一个列表,这个列表必须以1~N的顺序排列,并且父类不会排在子类后面,1含有一 ...
- P1494 [国家集训队]小Z的袜子(莫队算法)
莫队板子 代码 #include <cstdio> #include <algorithm> #include <cstring> #include <cma ...
- 4-Five-Youth
①People are always talking about 'the problem of youth'. If there is one--which I take leave to do ...
- EFI环境下的Ubuntu&Win10双系统安装
因为是win10是EFI启动的,所以网上的easyBCD方法就不可以用了,这里用到的不是ultraiso软碟通,用的哪个忘了 不过只要能写入U盘做成启动盘就ok 具体参考的是https://blog. ...
- HDU 4303 Hourai Jeweled(树形DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4303 题意:给出一棵树,树上的每一个节点都有一个权值,每条边有一个颜色,如果一条路径上相邻边的颜色都是不同的,那 ...