LeetCode028匹配字符串下标

68 阅读2分钟

题目:

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1

java:


/**
 * @author sunBo
 * @date 2025/11/29 21:01
 */
public class Leetcode028 {

    //滑动窗口.
    public static int Slide(String haystack, String needle) {
        int m = needle.length();
        //needle为空返回0.
        if (m == 0) {
            return 0;
        }
        int n = haystack.length();
        if (n < m) {
            return -1;
        }

        int i = 0;
        int j = 0;
        while (i < n - m + 1) {
            //找到首字母相等.
            while (haystack.charAt(i) != needle.charAt(j)) {
                i++;
            }
            //没有首字母相等.
            if (i == n) {
                return -1;
            }
            //遍历后续字符.
            i++;
            j++;
            while (i < n && j < m && haystack.charAt(i) == needle.charAt(j)) {
                i++;
                j++;
            }
            if (j == m) {
                //找到.
                return i - j;
            } else {
                //未找到.
                i -= j - 1;
                j = 0;
            }
        }
        return -1;
    }

    public static int nextStr(String haystack, String needle) {
        if (needle.length() == 0) {
            return 0;
        }

        int[] next = new int[needle.length()];
        getNext(next, needle);
        int j = -1;
        for (int i = 0; i < haystack.length(); i++) {
            while (j >= 0 && haystack.charAt(i) != needle.charAt(j + 1)) {
                j = next[j];
            }
            if (haystack.charAt(i) == needle.charAt(j + 1)) {
                j++;
            }
            if (j == needle.length() - 1) {
                return (i - needle.length() + 1);
            }
        }
        return -1;
    }

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

    public static void main(String[] args) {
        System.out.println(nextStr("mississippi", "issip"));
        //
        System.out.println(Slide("mississippi", "issip"));
    }
}

Go:


func Str(haystack, needle string) int {
	if len(needle) == 0 {
		return 0
	}
	next := make([]int, len(needle))
	getNext(next, needle)
	j := -1
	for i := 0; i < len(haystack); i++ {
		for j >= 0 && haystack[i] != needle[j+1] {
			j = next[j]
		}
		if haystack[i] == needle[j+1] {
			j++
		}
		if j == len(needle)-1 {
			return i - len(needle) + 1
		}
	}
	return -1
}

func getNext(next []int, needle string) {
	j := -1
	next[0] = j

	for i := 1; i < len(needle); i++ {
		for j > 0 && needle[i] != needle[j+1] {
			j = next[j]
		}

		if needle[i] == needle[j+1] {
			j++
		}
		next[i] = j
	}
}
	str1 := "mississippi"
	str2 := "issip"
	str := LeetCode.Str(str1, str2)
	fmt.Println(str)
}

贵在行.行后知.戒骄戒躁.

如果大家喜欢我的分享的话.可以关注我的微信公众号

念何架构之路