Skip to content

Commit 943a9ea

Browse files
committed
feat:0028.Find-the-Index-of-the-First-Occurrence-in-a-String
1 parent 2e70e52 commit 943a9ea

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: 0028. Find the Index of the First Occurrence in a String
3+
subtitle: "https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/"
4+
date: 2024-03-18T23:20:00+08:00
5+
lastmod: 2024-03-18T23:20:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0028.Find-the-Index-of-the-First-Occurrence-in-a-String"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy, Find the Index of the First Occurrence in a String,Two Pointers, String, String Matching]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [0028. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
70+
## Big O
71+
72+
* 時間複雜 : ``
73+
* 空間複雜 : ``
74+
75+
## 來源
76+
* https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
77+
* https://leetcode.cn/problems/
78+
79+
## 解答
80+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0028.Find-the-Index-of-the-First-Occurrence-in-a-String/main.go
81+
82+
```go
83+
84+
```
85+
86+
## Benchmark
87+
88+
```sh
89+
90+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package findtheindexofthefirstoccurrenceinastring
2+
3+
// 暴力解
4+
// 時間複雜 O(M*N), 空間複雜 O()
5+
func strStr(haystack string, needle string) int {
6+
haystackLen := len(haystack)
7+
needleLen := len(needle)
8+
index := 0
9+
for i := 0; i <= (haystackLen - needleLen); i++ {
10+
j := 0
11+
for j = 0; j < needleLen; j++ {
12+
if haystack[i+j] == needle[j] {
13+
index = i
14+
} else {
15+
break
16+
}
17+
}
18+
if j == needleLen {
19+
return index
20+
}
21+
}
22+
return -1
23+
}
24+
25+
// Slice 解法
26+
func strStrSlice(haystack string, needle string) int {
27+
haystackLen := len(haystack)
28+
needleLen := len(needle)
29+
if haystackLen == 0 || haystackLen < needleLen {
30+
return -1
31+
}
32+
if needleLen == 0 {
33+
return 0
34+
}
35+
for i := 0; i <= (haystackLen - needleLen); i++ {
36+
if haystack[i:i+needleLen] == needle {
37+
return i
38+
}
39+
}
40+
return -1
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 string
7+
want int
8+
}{
9+
{
10+
"bbbab",
11+
4,
12+
},
13+
}
14+
15+
func TestLongestPalindromeSubseq(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
19+
t.Errorf("got = %v, want = %v", got, tt.want)
20+
}
21+
}
22+
}
23+
24+
func BenchmarkLongestPalindromeSubseq(b *testing.B) {
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
LongestPalindromeSubseq(tests[0].arg1)
28+
}
29+
}
30+
31+
/*
32+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
33+
34+
*/

0 commit comments

Comments
 (0)