LeetCode_28---Implement strStr() [KMP算法,字符匹配算法]

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.

Hide Tags
  Two Pointers String
翻译: 有一个文本串S,和一个模式串P,现在要查找P在S中的位置,怎么查找呢?


Code:

/**
 * 
 */
package From21;

import java.util.Arrays;

/**
 * @author MohnSnow
 * @time 2015年6月12日 上午9:19:41
 * @KMP算法
 */
public class LeetCode28 {

	/**
	 * @param argsmengdx
	 *            -fnst
	 */
	//: Time Limit Exceeded
	public static int strStr(String haystack, String needle) {
		for (int i = 0; i < haystack.length(); i++) {
			int k = i;
			int j = 0;
			while (k < haystack.length() && j < needle.length()) {
				if (haystack.charAt(k) == needle.charAt(j)) {
					++k;
					++j;
				} else {
					break;
				}
			}
			if (j == needle.length()) {
				return i;
			}
		}
		return -1;
	}

	//: Time Limit Exceeded----进一步优化---多加几个合适的IF判断进一步减少特殊条件。---288msA
	public static int strStr1(String haystack, String needle) {
		if (needle.length() == 0)
			return 0;
		for (int i = 0; i < haystack.length(); i++) {
			if (haystack.charAt(i) == needle.charAt(0)) {
				int num = 1;
				for (int j = 1; j < needle.length(); j++) {
					if (i + j >= haystack.length())
						return -1;
					if (haystack.charAt(i + j) == needle.charAt(j)) {
						num++;
					}
				}
				if (num == needle.length())
					return i;
			}
		}
		return -1;
	}

	//https://leetcode.com/discuss/36420/accepted-java-solution---324msA
	//haystack.substring(i, i + needle.length()).equals(needle)使用已经存在的方法,达不到锻炼编程能力的效果,但是可以参考一下。
	public static int strStr2(String haystack, String needle) {
		if (haystack == null || needle == null || haystack.length() < needle.length()) {
			return -1;
		}
		if (needle.length() == 0) {
			return 0;
		}
		for (int i = 0; i < haystack.length(); i++) {
			if (haystack.charAt(i) == needle.charAt(0) && i + needle.length() <= haystack.length()
					&& haystack.substring(i, i + needle.length()).equals(needle)) {
				return i;
			}
		}
		return -1;
	}

	//字符串匹配算法一网打尽---http://dsqiu.iteye.com/blog/1700312
	//kmp算法---http://kb.cnblogs.com/page/176818/---http://www.acmerblog.com/kmp-algorithm-4407.html
	//利用空间取代时间的方法----http://www.ituring.com.cn/article/59881
	//结构之法算法之道位置---http://blog.csdn.net/v_july_v/article/details/7041827
	//next数组等于(上一个元素的最长前缀后缀公共元素长度,因为第一个吗,没有前任,所以赋予-1)
	public static int strStrKMP(String haystack, String needle) {
		int i = 0, j = 0;
		int[] next = new int[needle.length()];
		getNext(needle, next);//构建NEXT数组---寻找最长首尾匹配位置---最长首尾匹配子串长度
		System.out.println("构建NEXT数组:" + Arrays.toString(next));
		while (i < haystack.length() && j < needle.length()) {
			if (j == -1 || haystack.charAt(i) == needle.charAt(j)) {
				i++;
				j++;
			}
			else
				j = next[j];//比较位置后移
		}
		if (j == needle.length())
			return i - j;
		else
			return -1;
	}

	private static void getNext(String needle, int[] next) {
		int i = 0, j = -1;
		if (needle.length() == 0)
			return;
		next[0] = -1;
		while (i < needle.length() - 1) {
			if (j == -1 || needle.charAt(i) == needle.charAt(j)) {
				i++;
				j++;
				next[i] = j;
			} else {
				j = next[j];
			} 
		}
	}

	public static void main(String[] args) {
		String haystack = "bbcabcdababcdabcdabde";
		String needle = "abcd";
		//haystack = "mississippi";
		//needle = "issipi";
		System.out.println("开始位置:" + strStr(haystack, needle));
		System.out.println("开始位置1:" + strStr1(haystack, needle));
		System.out.println("开始位置2:" + strStr2(haystack, needle));
		System.out.println("开始位置KMP:" + strStrKMP(haystack, needle));
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值