[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;
}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-10-09 17:59:28

[LeetCode][Java] Implement strStr()的相关文章

[LeetCode] 028. Implement strStr() (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 028. Implement strStr() (Easy) 链接: 题目:https://oj.leetcode.com/problems/implement-strstr/ 代码(github):https://github.com/illuz/leetcode 题意: 在一个字符串里找另一个字符串在其中的位置

【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

Java for LeetCode 028 Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解题思路: 直接看代码即可,JAVA实现如下: static public int strStr(String haystack, String needle) { for(int i=0;i<=haystack.length()-needle.l

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: 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

Java [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. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

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

19.1.30 [LeetCode 28] Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

Leetcode练习题Implement strStr()

题目描述(easy) Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1 Input: haystack = "hello", needle = "ll" Output: 2 Example 2 Input: ha

【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