leetcode——Implement strStr() 实现字符串匹配函数(AC)

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

这个题考查的是KMP算法。先求特征向量,然后再进行匹配,确实能够大大提高效率。code例如以下:

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        if(strlen(haystack)==0&&strlen(needle)==0)
            return haystack;
        if(strlen(haystack)==0&&strlen(needle)!=0)
			return NULL;
		if(strlen(needle)==0)
		    return haystack;
		int m=strlen(needle);
        int *N = new int[m];
		N[0]=0;
		int i,j,k;
		for(i=1;i<m;i++)
		{
			k=N[i-1];
			while(k>0 && needle[i]!=needle[k])
			{
				k=N[k-1];
			}
			if(needle[i]==needle[k])
				N[i]=k+1;
			else
				N[i]=0;
		}
		j=0;
		for(i=0;i<strlen(haystack);i++)
		{
			while(j>0 && needle[j]!=haystack[i])
				j=N[j-1];
			if(needle[j]==haystack[i])
				j++;
			if(j==strlen(needle))
				return haystack+i-j+1;
		}
		return NULL;
    }
};
时间: 2024-08-04 04:30:15

leetcode——Implement strStr() 实现字符串匹配函数(AC)的相关文章

leetcode | Implement strStr() | 实现字符串查找函数

Implement strStr() : https://leetcode.com/problems/implement-strstr/ Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 如:haystack = "bcbcda"; needle = "bcd" 则 return 2 解析:字符串查找函数,

LeetCode: Implement strStr() [027]

[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. [题意] 实现库函数strStr(), 功能是在字符串haystack中找出目标串needle第一次出现的索引位 [思路]字符串的匹配,能够用暴力解法,但不推荐.一般使用KMP算法求解. 简要介绍一下KMP的思想: haystack是

[LeetCode] Implement strStr() [18]

题目 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 原题链接(点我) 解题思路 字符串匹配这也是个老题了,方法主要有下面4种, 1. 暴利破解法(BF),这个没啥说的,就是一轮一轮的比较,知道遇到相匹配的,这个的时间复杂度为O(n^2). 2. KMP,这应该是字符串匹配领域中最长听说的算

LeetCode 44 Wildcard Matching(字符串匹配问题)

题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). 字符串匹配问题. 如上所示:其中 ‘ ?’ 可以匹配任何一个单字符    ’ * ‘ 可以匹配任意长度的字符包括空字符串 给定字符串s,和字符串p.判

LeetCode Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you still

[Leetcode] implement strStr() (C++)

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02):The signature of the function had been updated to return the index instead of the pointer. If you st

Leetcode 686.重复叠加字符串匹配

重复叠加字符串匹配 给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = "cdabcdab". 答案为 3, 因为 A 重复叠加三遍后为 "abcdabcdabcd",此时 B 是其子串:A 重复叠加两遍后为"abcdabcd",B 并不是其子串. 注意: A 与 B 字符串的长度在1和10000区间范围内. 思

php -- strstr()字符串匹配函数(备忘)

Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() 函数搜索一个字符串在另一个字符串中的第一次出现. 该函数返回字符串的其余部分(从匹配点).如果未找到所搜索的字符串,则返回 false. string 必需.规定被搜索的字符串. search 必需.规定所搜索的字符串.如果该参数是数字,则搜索匹配数字 ASCII 值的字符. Example 1:

leetcode Implement strStr()(easy) /java

我以为,当时我用c++写这个函数的时候,整个人如同乱麻. 这次用java写.先查的SE 8中String的方法.找到两个与此函数有关的方法:matches()和substring(). import java.io.*; import java.util.*; public class Solution { public static int strStr(String haystack, String needle) { int r=-1; int len1=haystack.length()