#leetcode#Implement strStr()

原创 2015年07月07日 02:34:18

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.


这题brute force肯定要bug free, 如果能打出来O(n)的解法肯定加分, rolling hash解法必须掌握

下面的解法用3做base,Integer范围内不会溢出, 但是还是用Robin Karp的方法取余比较好, EPI上有这题的解法, 参悟一下再来update。

// https://jixiangsanbao.wordpress.com/2014/04/26/implement-strstr/
// http://blog.csdn.net/linhuanmars/article/details/20276833
public class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack == null || needle == null || needle.length() == 0){
            return 0;
        }
        if(haystack.length() < needle.length()){
            return -1;
        }
        int base = 3; // 如果用大的base, 比如 7 或者 29, 31, 数值太大会溢出, 导致hash计算不对
        // 比较细心的朋友可能看出来了,这个方法的hashcode比较容易越界,因为以素数为底的幂会很大,解决的办法可以用BigInteger,或者如同Rabin–Karp algorithm - Wikipedia一样对于hashcode进行取余,但是可能存在多个字符串映射到同一hashcode的问题,尽管是很少数的情况。

        int neeHash = getHash(needle, 0, needle.length() - 1, base);
        int hayHash = getHash(haystack, 0, needle.length() - 1, base);
        
        for(int i = 0; i <= haystack.length() - needle.length(); i++){
            // verify if collision
            if(hayHash == neeHash){
                if(haystack.substring(i, i + needle.length()).equals(needle)){
                    return i;
                }
            }
            // calculate new hash
            if(i != haystack.length() - needle.length()){
                hayHash -= ((int)(haystack.charAt(i))) * Math.pow(base, needle.length() - 1);
                hayHash *= base;
                hayHash += (int)(haystack.charAt(i + needle.length()));
            }
        }
        
        return -1;
    }
    
    private int getHash(String s, int start, int end, int base){
        int res = 0;
        int k = end - start;
        for(int i = start; i <= end; i++){
            char c = s.charAt(i);
            res += ((int)c) * Math.pow(base, k--);
        }
        return res;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 28. Implement strStr()(实现子串定位)

原题网址:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the...

Implement strStr() -- leetcode

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

[Leetcode P28] Implement strStr()(KMP算法)

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

LeetCode OJ Implement strStr()

Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if n...

《leetCode》:Implement strStr()

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

LeetCode OJ:Implement strStr()

Implement strStr()   Implement strStr(). Returns a pointer to the first occurrence of needle in h...

LeetCode(28)Implement Strstr()

题目如下 Implement strStr()  Returns a pointer to the first occurrence of needle in haystack, or null if...

LeetCode: Implement strStr() [027]

【题目】 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null...

LeetCode 28 Implement strStr() 找到字串返回位置。

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

[Leetcode]_28 Implement strStr()

Leetcode_28 Implement strStr() 字符串模式匹配 BF KMP
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)