leetcode 28. 实现 strStr() by lbh

java
先搞笑一波
最快

class Solution {
    public int strStr(String haystack, String needle) {
        return haystack.indexOf(needle);
    }
}

java

class Solution {
    public int strStr(String haystack, String needle) {
        int hlen=haystack.length();
        int nlen=needle.length();
        if(hlen==nlen&&haystack.equals(needle)) {
        	return 0;
        }
        if(hlen<nlen)return -1;
        for(int i=0;i<=hlen-nlen;i++) {
        	int j;
        	for(j=0;j<nlen;j++) {
        		if(haystack.charAt(j+i)!=needle.charAt(j))break;
        	}
        	if(j==nlen) {
        		return i;
        	}
        }
        return -1;
    }
}

indexof源码并不快

class Solution {
    public  int indexOf(String source, String target, int fromIndex) {
        final int sourceLength = source.length();
        final int targetLength = target.length();
        if (fromIndex >= sourceLength) {
            return (targetLength == 0 ? sourceLength : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetLength == 0) {
            return fromIndex;
        }

        char first = target.charAt(0);
        // 计算出最多比较的次数
        int max = (sourceLength - targetLength);

        for (int i = fromIndex; i <= max; i++) {
            // 寻找在source中出现和target第一个字符相等的位置
            if (source.charAt(i)!= first) {
                while (++i <= max && source.charAt(i) != first);
            }

            if (i <= max) {
                // 找到第一个相等的字符后,从下一个字符开始再比较(下次比较开始的位置)
                int j = i + 1;
                // 除target第一个字符,剩下字符再比较结束的位置
                // 可以理解为:j+(targetLength-1), 即开始的位置+ target剩下要比较字符的长度
                int end = j + targetLength - 1;
                /* j < end 说明还没有比较完
                 * j < end && source.charAt(j) == target.charAt(k) 是真说明在还没比较完的情况下比较的字符相等,
                 *  那么继续循环,直到条件为false
                 */
                for (int k = 1; j < end && source.charAt(j) == target.charAt(k); j++, k++);

                // 上面循环结束时 j刚好等于结束比较的位置,那么就返回上面找到的target第一个字符相等的位置
                if (j == end) {
                    return i;
                }
            }
        }
        return -1;
    }
	public int strStr(String haystack, String needle) {
        return indexOf(haystack, needle, 0);
    }
}

kmp 更慢一些

class Solution {
    public static int[] next(char[] t) {  
        int[] next = new int[t.length];  
        next[0] = -1;  
        int i = 0;  
        int j = -1;  
        while (i < t.length - 1) {  
            if (j == -1 || t[i] == t[j]) {  
                i++;  
                j++;  
                if (t[i] != t[j]) {  
                    next[i] = j;  
                } else {  
                    next[i] = next[j];  
                }  
            } else {  
                j = next[j];  
            }  
        }  
        return next;  
    }  
    public static int KMP_Index(char[] s, char[] t) {  
        int[] next = next(t);  
        int i = 0;  
        int j = 0;  
        while (i <= s.length - 1 && j <= t.length - 1) {  
            if (j == -1 || s[i] == t[j]) {  
                i++;  
                j++;  
            } else {  
                j = next[j];  
            }  
        }  
        if (j < t.length) {  
            return -1;  
        } else  
            return i - t.length; // 返回模式串在主串中的头下标   
    }  
	public int strStr(String haystack, String needle) {
		if(haystack.length()<needle.length())return -1;
		if(0==needle.length())return 0;
        return KMP_Index(haystack.toCharArray(), needle.toCharArray());
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值