Skip to content

Latest commit

 

History

History
45 lines (39 loc) · 527 Bytes

28.实现-str-str.md

File metadata and controls

45 lines (39 loc) · 527 Bytes
title categories
实现 strStr()
leetcode
leetcode题解
package main

/*
 * @lc app=leetcode.cn id=28 lang=golang
 *
 * [28] 实现 strStr()
 */

// hellabcelp
//  elp

// @lc code=start
func strStr(haystack string, needle string) int {
	if len(needle) == 0 {
		return 0
	}
	i, j := 0, 0
	for j < len(haystack) {

		c := needle[i]
		ch := haystack[j]
		if c == ch {
			i++
			j++
		} else {
			j = j - i + 1
			i = 0
		}
		if i == len(needle) {
			return j - i
		}

	}
	return -1
}

// @lc code=end