跳到主要内容

28-找出字符串中第一个匹配项的下标

题目描述

找出字符串中第一个匹配项的下标

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

/*
* @lc app=leetcode.cn id=28 lang=golang
*
* [28] 找出字符串中第一个匹配项的下标
*/

// @lc code=start
func strStr(haystack string, needle string) int {
if needle == "" {
return 0
}
if len(haystack) < len(needle) {
return -1
}
for i := 0; i < len(haystack); i++ {
if i+len(needle) > len(haystack) {
return -1
}
if haystack[i:i+len(needle)] == needle {
return i
}
}
return -1
}

// @lc code=end