[leecode]Implement strStr()

Implement strStr()

Implement strStr().

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

实现String.class中的contains()接口。

KMP算法

不了解该算法的,请移步KMP算法小结。这个也属于必须要掌握的算法。

【未通过case】needle == “”

代码如下:

public class Solution {
    public String strStr(String haystack, String needle) {
        if(haystack == null || needle == null || haystack.length() < needle.length() || (haystack.length() == needle.length() && !haystack.equals(needle)) ) return null;//needle为空串要特别注意
        if(needle.equals("") || haystack.equals(needle)) return haystack;
        int i = 0, j = 0;
        int[] next = new int[needle.length()];
        setNext(needle,next);
        while(i < haystack.length()){
            if(j == -1 || haystack.charAt(i) == needle.charAt(j)){
                i++;
                j++;
            }else{
                j = next[j];
            }
            if(j == needle.length()) return haystack.substring(i - needle.length());
        }
        return null;
    }
    private void setNext(String needle,int[] next){
        char[] p = needle.toCharArray();
        int length = p.length;
        int k = -1, j = 0;
        next[0] = -1;
        while(j < length - 1){
            if(k == -1 || p[k] == p[j]){
                next[++j] = ++k;
            }else{
                k = next[k];
            }
        }
    }

}

这道题我整理了整整一天,第一次手动的实现了KMP,并作出KMP算法小结。后天还要再复习一下。

时间: 2024-10-13 19:11:33

[leecode]Implement strStr()的相关文章

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

Implement strStr()

package cn.edu.xidian.sselab.string; /** *  * @author zhiyong wang * title: Implement strStr() * content: *  Implement strStr(). *   *  Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  * */pub

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【leetcode】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

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

Implement strStr()——字符串中第一次出现给定字符串的位置

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41452047 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 func

28. Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 非常容易的一道题目,注意各种特殊情况,先列举出corner case,再进行编码.学习参考经典实现. 这种关于字符串操作类的题目在面试时必须能够倒背如流. int strStr(char* haystack, char* needle) { if

44. leetcode 28. Implement strStr()

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.

【LeetCode】28. Implement strStr() 解题小结

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 简单题. class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; if (haystack.em