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算法。可参考下文。

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)的更多相关文章

  1. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

  2. 【LeetCode】28 - Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  4. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. 【LeetCode】28. 实现 strStr()

    28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...

  6. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  7. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  8. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  9. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

随机推荐

  1. linux nfs服务配置挂载以及oracle使用nfs存储挂载注意事项

    服务端共享目录 /home/XXX/nfs_shared 172.16.22.0/24(rw,no_root_squash) service nfs restart 常用命令: 查看所有nfs共享目录 ...

  2. centos/rhel 7 几个最重要变化(systemd,firewalld,networkmanager,文件系统)

    详细参考:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administra ...

  3. python之字符编码(三)

    一.字符编码的分类: 计算机由美国人发明,最早的字符编码为ASCII,只规定了英文字母数字和一些特殊字符与数字的对应关系.最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCI ...

  4. 04: vue生命周期和实例属性和方法

    1.4 组件的生命周期 1.说明 1. Vue将组件看成是一个有生命的个体,跟人一样,定义了各个阶段, 2. 组件的生命周期:组件的创建过程 3. 组件生命周期钩子函数:当组件处在某个阶段,要执行某个 ...

  5. Oracle为表或字段添加备注

    comment on column TableName.ColumnName is ‘备注名’; comment on table TableName is '备注名';

  6. Python3 tkinter基础 Canvas create_text 在画布上添加文字

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Flutter的脚手架(Scaffold)

  8. POJ 2018 Best Cow Fences(二分最大区间平均数)题解

    题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这 ...

  9. (zhuan) Variational Autoencoder: Intuition and Implementation

    Agustinus Kristiadi's Blog TECH BLOG TRAVEL BLOG PORTFOLIO CONTACT ABOUT Variational Autoencoder: In ...

  10. (转) The Incredible PyTorch

    转自:https://github.com/ritchieng/the-incredible-pytorch The Incredible PyTorch What is this? This is ...