LeetCode28. 实现strStr()Golang版实现 strStr() 函数。给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。 2. 思路3...
LeetCode28. 实现strStr()Golang版
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

2. 思路
3. 代码
func strStr(haystack string, needle string) int {
if needle == "" {
return 0
}
if len(needle) > len(haystack) {
return -1
}
j := 0
var ii int
var index int
var length int
for i := 0; i < len(haystack); i++ {
ii = i
length = 0
for j = 0; j < len(needle); j++ {
if ii >= len(haystack) {
return -1
}
if needle[j] == haystack[ii] {
ii++
length++
}
}
if length == len(needle) {
index = i
break
}
}
if length == len(needle) {
return index
} else {
return -1
}
return -1
}
织梦狗教程
本文标题为:LeetCode28. 实现strStr()Golang版
基础教程推荐
猜你喜欢
- ruby on rails validates 2023-09-22
- win10下使用virtualbox + vagrant配置ruby开发机环境 2023-07-23
- 浅析ELF转二进制允许把 Binary 文件加载到任意位置 2023-07-06
- Swift中重写和重载的使用与对比总结 2023-07-05
- ruby-on-rails-为使用Rails 4,nginx和乘客的用户设置自定义域 2023-09-21
- R语言 ggplot2改变柱状图的顺序操作 2022-11-17
- R语言-修改(替换)因子变量的元素操作 2022-11-26
- R语言绘制折线图实例分析 2022-11-21
- Ruby3多线程并行Ractor使用方法详解 2023-07-23
- Swift初始化器与可选链的使用方法介绍 2023-07-08
