|
1 |
| -# 28. Find the Index of the First Occurrence in a String - Best Practices of LeetCode Solutions |
2 |
| -LeetCode English link: [28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string) |
3 |
| - |
4 |
| -LeetCode Chinese link: [28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string) |
5 |
| - |
6 |
| -[中文题解](#中文题解) |
7 |
| - |
8 |
| -## LeetCode problem description |
9 |
| -Given two strings `needle` and `haystack`, return the **index** of the first occurrence of `needle` in `haystack`, or `-1` if `needle` is not part of `haystack`. |
| 1 | +# 28. 找出字符串中第一个匹配项的下标 - 力扣题解最佳实践 |
| 2 | +力扣链接:[28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string), 难度: **简单**。 |
10 | 3 |
|
11 |
| -Difficulty: **Easy** |
| 4 | +## 力扣“28. 找出字符串中第一个匹配项的下标”问题描述 |
| 5 | +给你两个字符串 `haystack` 和 `needle` ,请你在 `haystack` 字符串中找出 `needle` 字符串的第一个匹配项的下标(下标从 `0` 开始)。如果 `needle` 不是 `haystack` 的一部分,则返回 `-1` 。 |
12 | 6 |
|
13 |
| -### [Example 1] |
14 |
| -**Input**: `haystack = "sadbutsad", needle = "sad"` |
| 7 | +### [示例 1] |
| 8 | +**输入**: `haystack = "sadbutsad", needle = "sad"` |
15 | 9 |
|
16 |
| -**Output**: `0` |
| 10 | +**输出**: `0` |
17 | 11 |
|
18 |
| -**Explanation**: |
| 12 | +**解释**: |
19 | 13 | ```
|
20 |
| -"sad" occurs at index 0 and 6. |
21 |
| -The first occurrence is at index 0, so we return 0. |
| 14 | +"sad" 在下标 0 和 6 处匹配。 |
| 15 | +第一个匹配项的下标是 0 ,所以返回 0 。 |
22 | 16 | ```
|
23 | 17 |
|
24 |
| -### [Example 2] |
25 |
| -**Input**: `haystack = "leetcode", needle = "leeto"` |
| 18 | +### [示例 2] |
| 19 | +**输入**: `haystack = "leetcode", needle = "leeto"` |
26 | 20 |
|
27 |
| -**Output**: `-1` |
| 21 | +**输出**: `-1` |
28 | 22 |
|
29 |
| -**Explanation**: `"leeto" did not occur in "leetcode", so we return -1.` |
| 23 | +**解释**: `"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。` |
30 | 24 |
|
31 |
| -### [Constraints] |
| 25 | +### [约束] |
32 | 26 | - `1 <= haystack.length, needle.length <= 10000`
|
33 |
| -- `haystack` and `needle` consist of only lowercase English characters. |
34 |
| - |
35 |
| -## Intuition |
36 |
| -[中文题解](#中文题解) |
| 27 | +- `haystack` 和 `needle` 仅由小写英文字符组成 |
37 | 28 |
|
38 |
| -Traverse the string once, and if the `needle.length` characters after the current position are equal to `needle`, return the current `index`. |
| 29 | +## 思路 |
| 30 | +遍历一遍字符串,如果从当前位置起的后的`needle.length`个字符等于`needle`,则返回当前的`index`。 |
39 | 31 |
|
40 |
| -## Complexity |
41 |
| -* Time: `O(m * n)`. |
42 |
| -* Space: `O(n)`. |
| 32 | +## 复杂度 |
| 33 | +* 时间:`O(m * n)`。 |
| 34 | +* 空间:`O(n)`。 |
43 | 35 |
|
44 | 36 | ## Python
|
45 | 37 | ```python
|
@@ -94,22 +86,3 @@ var strStr = function (haystack, needle) {
|
94 | 86 | ```
|
95 | 87 | // Welcome to create a PR to complete the code of this language, thanks!
|
96 | 88 | ```
|
97 |
| - |
98 |
| -## 力扣“28. 找出字符串中第一个匹配项的下标”问题描述 |
99 |
| -力扣链接:[28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string), 难度: **简单**。 |
100 |
| - |
101 |
| -给你两个字符串 `haystack` 和 `needle` ,请你在 `haystack` 字符串中找出 `needle` 字符串的第一个匹配项的下标(下标从 `0` 开始)。如果 `needle` 不是 `haystack` 的一部分,则返回 `-1` 。 |
102 |
| - |
103 |
| -### [示例 1] |
104 |
| -**输入**: `haystack = "sadbutsad", needle = "sad"` |
105 |
| - |
106 |
| -**输出**: `0` |
107 |
| - |
108 |
| -### [示例 1] |
109 |
| -**输入**: `haystack = "leetcode", needle = "leeto"` |
110 |
| - |
111 |
| -**输出**: `-1` |
112 |
| - |
113 |
| -# 中文题解 |
114 |
| -## 思路 |
115 |
| -遍历一遍字符串,如果从当前位置起的后的`needle.length`个字符等于`needle`,则返回当前的`index`。 |
0 commit comments