Skip to content

Commit 996f17c

Browse files
committed
一刷28
1 parent cc30d3c commit 996f17c

File tree

6 files changed

+99
-46
lines changed

6 files changed

+99
-46
lines changed

README.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
223223
|
224224

225225
|{counter:codes}
226-
|{leetcode_base_url}/implement-strstr/[28. Implement strStr()^]
227-
|{source_base_url}/_0028_ImplementStrStr.java[Java]
228-
|{doc_base_url}/0028-implement-strstr.adoc[题解]
226+
|{leetcode_base_url}/find-the-index-of-the-first-occurrence-in-a-string/[28. 找出字符串中第一个匹配项的下标]
227+
|{source_base_url}/_0028_FindTheIndexOfTheFirstOccurrenceInAString.java[Java]
228+
|{doc_base_url}/0028-find-the-index-of-the-first-occurrence-in-a-string.adoc[题解]
229229
|Easy
230230
|
231231

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[#0028-find-the-index-of-the-first-occurrence-in-a-string]
2+
= 28. 找出字符串中第一个匹配项的下标
3+
4+
https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/[LeetCode - 28. 找出字符串中第一个匹配项的下标 ^]
5+
6+
给你两个字符串 `haystack``needle` ,请你在 `haystack` 字符串中找出 `needle` 字符串的第一个匹配项的下标(下标从 0 开始)。如果 `needle` 不是 `haystack` 的一部分,则返回 `-1`
7+
8+
*示例 1:*
9+
10+
....
11+
输入:haystack = "sadbutsad", needle = "sad"
12+
输出:0
13+
解释:"sad" 在下标 0 和 6 处匹配。
14+
第一个匹配项的下标是 0 ,所以返回 0 。
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入:haystack = "leetcode", needle = "leeto"
21+
输出:-1
22+
解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
23+
....
24+
25+
*提示:*
26+
27+
* `1 \<= haystack.length, needle.length \<= 10^4^`
28+
* `haystack``needle` 仅由小写英文字符组成
29+
30+
31+
== 思路分析
32+
33+
34+
[[src-0028]]
35+
[tabs]
36+
====
37+
一刷::
38+
+
39+
--
40+
[{java_src_attr}]
41+
----
42+
include::{sourcedir}/_0028_FindTheIndexOfTheFirstOccurrenceInAString.java[tag=answer]
43+
----
44+
--
45+
46+
// 二刷::
47+
// +
48+
// --
49+
// [{java_src_attr}]
50+
// ----
51+
// include::{sourcedir}/_0028_FindTheIndexOfTheFirstOccurrenceInAString_2.java[tag=answer]
52+
// ----
53+
// --
54+
====
55+
56+
57+
== 参考资料
58+
59+

docs/0028-implement-strstr.adoc

Lines changed: 0 additions & 41 deletions
This file was deleted.

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ include::0026-remove-duplicates-from-sorted-array.adoc[leveloffset=+1]
138138

139139
include::0027-remove-element.adoc[leveloffset=+1]
140140

141-
include::0028-implement-strstr.adoc[leveloffset=+1]
141+
include::0028-find-the-index-of-the-first-occurrence-in-a-string.adoc[leveloffset=+1]
142142

143143
include::0029-divide-two-integers.adoc[leveloffset=+1]
144144

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ endif::[]
230230
|{doc_base_url}/0092-reverse-linked-list-ii.adoc[题解]
231231
|✅ 链表反转
232232

233-
|===
233+
|{counter:codes2503}
234+
|{leetcode_base_url}/find-the-index-of-the-first-occurrence-in-a-string/[28. 找出字符串中第一个匹配项的下标]
235+
|{doc_base_url}/0028-find-the-index-of-the-first-occurrence-in-a-string.adoc[题解]
236+
|✅ 暴力破解。更有的解法是 KMP 算法、Boyer-Moore 算法、Sunday 算法等算法。
234237

238+
|===
239+
˚
235240
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0028_FindTheIndexOfTheFirstOccurrenceInAString {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-11 21:02:03
8+
*/
9+
public int strStr(String haystack, String needle) {
10+
int m = haystack.length(), n = needle.length();
11+
char[] hc = haystack.toCharArray();
12+
char[] nc = needle.toCharArray();
13+
for (int i = 0; i + n < m; i++) {
14+
int x = i, y = 0;
15+
while (y < n && hc[x] == nc[y]) {
16+
x++;
17+
y++;
18+
}
19+
if (y == n) {
20+
return i;
21+
}
22+
}
23+
return -1;
24+
}
25+
// end::answer[]
26+
public static void main(String[] args) {
27+
new _0028_FindTheIndexOfTheFirstOccurrenceInAString()
28+
.strStr("a", "a");
29+
}
30+
}

0 commit comments

Comments
 (0)