Skip to content

Commit 8bcc39c

Browse files
committed
docs: 完全同步第28题英文版到 leetcoder.net 的原始内容
1 parent 503cec5 commit 8bcc39c

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
Original link: [leetcoder.net - LeetCoder: Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
22

33
# 28. Find the Index of the First Occurrence in a String - LeetCoder: Fucking Good LeetCode Solutions
4+
45
LeetCode 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), Difficulty: **Easy**.
56

67
## LeetCode description of "28. Find the Index of the First Occurrence in a String"
8+
79
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`.
810

911
### [Example 1]
12+
1013
**Input**: `haystack = "sadbutsad", needle = "sad"`
1114

1215
**Output**: `0`
1316

14-
**Explanation**:
15-
```
17+
**Explanation**:
18+
1619
"sad" occurs at index 0 and 6.
1720
The first occurrence is at index 0, so we return 0.
18-
```
1921

2022
### [Example 2]
23+
2124
**Input**: `haystack = "leetcode", needle = "leeto"`
2225

2326
**Output**: `-1`
2427

25-
**Explanation**: `"leeto" did not occur in "leetcode", so we return -1.`
28+
**Explanation**:
29+
30+
"leeto" did not occur in "leetcode", so we return -1.
2631

2732
### [Constraints]
33+
2834
- `1 <= haystack.length, needle.length <= 10000`
2935
- `haystack` and `needle` consist of only lowercase English characters.
3036

3137
## Intuition
38+
3239
- This kind of question can be solved with one line of code using the built-in `index()`. Obviously, the questioner wants to test our ability to control the loop.
3340

3441
- For `heystack`, traverse each character in turn. There may be two situations:
@@ -38,39 +45,49 @@ The first occurrence is at index 0, so we return 0.
3845
- This question is easier to understand by looking at the code directly.
3946

4047
## Complexity
48+
4149
- Time complexity: `O(N + M)`.
4250
- Space complexity: `O(1)`.
4351

4452
## Python
53+
4554
```python
4655
class Solution:
4756
def strStr(self, haystack: str, needle: str) -> int:
4857
for i in range(len(haystack)):
4958
j = 0
59+
5060
while i + j < len(haystack) and haystack[i + j] == needle[j]:
5161
j += 1
62+
5263
if j == len(needle):
5364
return i
65+
5466
return -1
5567
```
5668

5769
## JavaScript
70+
5871
```javascript
5972
var strStr = function (haystack, needle) {
6073
for (let i = 0; i < haystack.length; i++) {
6174
let j = 0
75+
6276
while (i + j < haystack.length && haystack[i + j] == needle[j]) {
63-
j += 1
64-
if (j == needle.length) {
65-
return i
66-
}
77+
j += 1
78+
79+
if (j == needle.length) {
80+
return i
81+
}
6782
}
6883
}
84+
6985
return -1
7086
};
7187
```
7288

7389
## Other languages
90+
7491
```java
7592
// Welcome to create a PR to complete the code of this language, thanks!
7693
```

0 commit comments

Comments
 (0)