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 see your function signature returns a char
*
 or String, please click the reload button  to
reset your code definition.

题意:找到第一次匹配的下标。

思路:KMP模板题。

class Solution {
public:
    void getNext(char *patten, vector<int> &next) {
        int i = 0, j = -1;
        int len = strlen(patten);
        next.resize(len+1);
        next[0] = -1;
        while (i < len) {
            if (j == -1 || patten[i] == patten[j]) {
                i++, j++;
                next[i] = j;
            } else j = next[j];
        }
    }

    int strStr(char *haystack, char *needle) {
        if (haystack == NULL || needle == NULL)
            return -1;

        int i = 0, j = 0;
        int lena = strlen(haystack);
        int lenb = strlen(needle);
        vector<int> next;
        getNext(needle, next);
        while (i < lena && j < lenb) {
            if (j == -1 || haystack[i] == needle[j])
                i++, j++;
            else j = next[j];

        }
        if (j == lenb)
            return i - j;
        else return -1;
    }
};
时间: 2025-01-02 18:32:31

LeetCode Implement strStr()的相关文章

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 | 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() (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——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(

leetcode implement strStr python

#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if len(haystack) <= 0 and len(needle)<=0: return 0 arrNext=self.getNext(needle) i=0 j=0 intHLen=

LeetCode Implement strStr() 实现strstr()

如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ 1 int strStr(char* haystack, char* needle) { 2 if (!haystack || !needle) return -1; 3 for (int i = 0; ; ++i) { 4 for (int j = 0; ; ++j) { 5 if (needle[j] == 0) return i; 6 if (haystack[i + j] == 0) return -1; 7 if

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()

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co