[LeetCode][Java] Implement strStr()

题目:

Implement strStr().

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

题意:

实现strStr()

返回needle在haystack中第一次出现的位置,如果haystack中不存在needle,返回-1.

算法分析:

方法一:

 * in Java, there is an API function name indexof(),

 * it returns index within this string of the first occurrence of the specifiedsubstring. 

方法二:

 * 最native的做法

 * 解题想法是,从haystack的第一个位置,开始逐个判断是不是子串。如果整个子串都匹配了,那么就返回,否则继续往下挪位置。

 * 注意要看haystack剩余的长度跟needle比足不足够多,不够的话也就不用往后比了。

方法三:

 * 利用看毛片(KMP)算法,但是对于一个easy难度的题,利用这么费脑的算法没必要吧~~

 * 有兴趣的参考《从头到尾彻底理解KMP》http://blog.csdn.net/v_july_v/article/details/7041827

 * 代码也在下面给出来。

AC代码:

方法一:

//in Java, there is an API function name indexof(),
//it returns index within this string of the first occurrence of the specifiedsubstring. 
public class Solution 
{
    public int strStr(String haystack, String needle) 
    {
        int i;
        i=haystack.indexOf(needle);
        return i;
    }
}

方法二:


/**
 * 最native的做法
 * 解题想法是,从haystack的第一个位置,开始逐个判断是不是子串。如果整个子串都匹配了,那么就返回,否则继续往下挪位置。
 * 注意要看haystack剩余的长度跟needle比足不足够多,不够的话也就不用往后比了。
 */ 
public class Solution
{
    public int strStr(String haystack, String needle) 
    {
        if(haystack==null || needle==null)    
            return 0;
     
        if(needle.length() == 0)
            return 0;
     
        for(int i=0; i<haystack.length(); i++)
        {
            if(i + needle.length() > haystack.length())
                return -1;
     
            int m = i;
            for(int j=0; j<needle.length(); j++)
            {
                if(needle.charAt(j)==haystack.charAt(m))
                {
                    if(j==needle.length()-1)
                        return i;
                    m++;
                }
                else
                {
                    break;
                }
            }    
        }   
        return -1;
    }
}
方法三:

public int strStr(String haystack, String needle) {
        if(haystack==null || needle==null)    
            return 0;
 
	int h = haystack.length();
	int n = needle.length();
 
	if (n > h)
		return -1;
	if (n == 0)
		return 0;
 
	int[] next = getNext(needle);
	int i = 0;
 
	while (i <= h - n) {
		int success = 1;
		for (int j = 0; j < n; j++) {
			if (needle.charAt(0) != haystack.charAt(i)) {
				success = 0;
				i++;
				break;
			} else if (needle.charAt(j) != haystack.charAt(i + j)) {
				success = 0;
				i = i + j - next[j - 1];
				break;
			}
		}
		if (success == 1)
			return i;
	}
 
	return -1;
}
 
//calculate KMP array
public int[] getNext(String needle) {
	int[] next = new int[needle.length()];
	next[0] = 0;
 
	for (int i = 1; i < needle.length(); i++) {
		int index = next[i - 1];
		while (index > 0 && needle.charAt(index) != needle.charAt(i)) {
			index = next[index - 1];
		}
 
		if (needle.charAt(index) == needle.charAt(i)) {
			next[i] = next[i - 1] + 1;
		} else {
			next[i] = 0;
		}
	}
 
	return next;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值