【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 ...
随机推荐
- Docker学习笔记之Docker 的简历
0x00 概述 在了解虚拟化和容器技术后,我们就更容易理解 Docker 的相关知识了.在这一小节中,我将介绍关于 Docker 的出现和发展,Docker 背后的技术.同时,我们将阐述 Docker ...
- Centos环境自写脚本查看使用php或nginx占用内存
在CentOs6.4下,用root权限测试. # cd ~ //进入home目录 # vim .bashrc //编辑文件,把下面代码放入地址 mem () { top -n1 -b | head - ...
- ReentrantLock$Sync.tryRelease java.lang.IllegalMonitorStateException
早上一来,例行性的看主要环境的运行情况,发现有个环境中有如下异常: 17-02-28 08:13:37.368 ERROR pool-2-thread-65 com.ld.net.spider.Spi ...
- Excel vba map/dictionary
今天在调整一生成java代码的工具时,要用到在不同语言间互转数据类型的逻辑,原来的代码中根据excel记录的行号来计算,再到另外一个collection中获取,个人想着这也太土鳖了,于是搜了下,在vb ...
- 理解 neutron
之前大师发个结构图. understanding_neutron.pdf 自己走读了代码: 1. get_extensions_path() # 在/opt/stack/neutron/neutro ...
- Fiddler抓取手机端(ios+android)APP接口数据(http+https)
(1)android 环境要求: PC机和手机连接在同一网络下 工具下载地址: Fiddler网上可以下载,自行下载.注意:需要安装fiddlercertmaker(网上自行下载)进行认证 配置步骤: ...
- bzoj 3522 / 4543 [POI 2014] Hotel - 动态规划 - 长链剖分
题目传送门 bzoj 3522 需要root权限的传送点 bzoj 4543 快速的传送点 慢速的传送点 题目大意 给定一棵树,问有多少个无序三元组$(x, y, z)$使得这三个不同点在树上两两距离 ...
- Django使用多数据库
有些项目可能涉及到使用多个数据库的情况,方法很简单. 1.在settings中设定DATABASE 比如要使用两个数据库: DATABASES = { 'default': { 'NAME': 'ap ...
- 一、变量.二、过滤器(filter).三、标签(tag).四、条件分支tag.五、迭代器tag.六、自定义过滤器与标签.七、全系统过滤器(了解)
一.变量 ''' 1.视图函数可以通过两种方式将变量传递给模板页面 -- render(request, 'test_page.html', {'变量key1': '变量值1', ..., '变量ke ...
- [WARNING]: Could not match supplied host pattern, ignoring: servers
Centos7.5 ansible执行命令报错 问题: [root@m01 ~]# ansible servers -a "hostname" [WARNING]: provide ...