【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 ...
随机推荐
- js 解密 16进制转10进制,再取ascii码的对应值
如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...
- entry points
https://amir.rachum.com/blog/2017/07/28/python-entry-points/
- Android之Sqlite数据库
数据库访问完毕后,游标必须也记得关闭 import com.huangzhong.love_power_model.UserInfoDto; import java.util.ArrayList; i ...
- bzoj 4585 烟火表演 - 动态规划 - 可并堆
题目传送门 传送门I 传送门II 题目大意 给定一棵带边权有根树,修改一条边的边权的代价是修改前和修改后的值的绝对值之差.不能将一条边的边权改为负数.问使得根节点到所有叶节点的距离相等的最小代价. 当 ...
- Xcode project 设置相关
FauxPas 这是一款Mac平台的用于检查Xcode项目的辅助工具 ,可以帮助我们找出常见的错误.隐藏的bug.不良实践以及可维护性问题和风格问题. 一, $(SRCROOT) :当前工程所在的目 ...
- QWidget设置背景图
1.使用QSS出现很多问题 2.方法 this->setAutoFillBackground(true); QPalette palette = this->palette(); pale ...
- CXF整合spring,在tomcat中发布webService
服务端 1.首先下载CXF的jar包 http://pan.baidu.com/s/1dFBwSRf 密码: qyax.里面自带了需要用到的spring的jar包. 或者使用maven,如下配置.不论 ...
- ubuntu18.04无法安装libesd0-dev【学习笔记】
执行如下命令安装: sudo apt-get install libesd0-dev 却报了这个错误: 解决办法: sudo vim /etc/apt/sources.list //在行尾添加如下两行 ...
- 待续未完- 自己写后台内容管理程序 - 用tp框架 和 前台 jquery ui等写的
在日常开发中,我们主要使用的还是 php 的 内部的 语言本身提供的函数/常量,系统内部数组等. 为了和后面的tp框架提供的 "系统函数.系统常量"相区别,把php提供的东西叫语言 ...
- LuoguP3792 由乃与大母神原型和偶像崇拜
题目地址 题目链接 题解 由乃题还是毒瘤啊orz 显然的一个结论是,如果保证不重复,维护区间min,max然后判断max-min+1==r-l+1是否成立即可 但是有重复 于是就要orz题解区的各位大 ...