Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 503cec5

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

File tree

1 file changed

+17
-38
lines changed

1 file changed

+17
-38
lines changed
Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Original link: [leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string)
1+
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

3-
# 28. Find the Index of the First Occurrence in a String - Fucking Good LeetCode Solutions
4-
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**.
3+
# 28. Find the Index of the First Occurrence in a String - LeetCoder: Fucking Good LeetCode Solutions
4+
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**.
55

66
## LeetCode description of "28. Find the Index of the First Occurrence in a String"
77
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`.
@@ -29,7 +29,6 @@ The first occurrence is at index 0, so we return 0.
2929
- `haystack` and `needle` consist of only lowercase English characters.
3030

3131
## Intuition
32-
3332
- 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.
3433

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

4140
## Complexity
42-
* Time: `O(m + n)`.
43-
* Space: `O(n)`.
41+
- Time complexity: `O(N + M)`.
42+
- Space complexity: `O(1)`.
4443

4544
## Python
4645
```python
4746
class Solution:
4847
def strStr(self, haystack: str, needle: str) -> int:
4948
for i in range(len(haystack)):
50-
if haystack[i:i + len(needle)] == needle:
51-
return i
52-
49+
j = 0
50+
while i + j < len(haystack) and haystack[i + j] == needle[j]:
51+
j += 1
52+
if j == len(needle):
53+
return i
5354
return -1
5455
```
5556

5657
## JavaScript
5758
```javascript
5859
var strStr = function (haystack, needle) {
5960
for (let i = 0; i < haystack.length; i++) {
60-
if (haystack.slice(i, i + needle.length) == needle) {
61-
return i
61+
let j = 0
62+
while (i + j < haystack.length && haystack[i + j] == needle[j]) {
63+
j += 1
64+
if (j == needle.length) {
65+
return i
66+
}
6267
}
6368
}
64-
6569
return -1
6670
};
6771
```
6872

69-
## C++
70-
```cpp
71-
// Welcome to create a PR to complete the code of this language, thanks!
72-
```
73-
74-
## Java
73+
## Other languages
7574
```java
7675
// Welcome to create a PR to complete the code of this language, thanks!
7776
```
78-
79-
## C#
80-
```c#
81-
// Welcome to create a PR to complete the code of this language, thanks!
82-
```
83-
84-
## Go
85-
```go
86-
// Welcome to create a PR to complete the code of this language, thanks!
87-
```
88-
89-
## Ruby
90-
```ruby
91-
# Welcome to create a PR to complete the code of this language, thanks!
92-
```
93-
94-
## C, Kotlin, Swift, Rust or other languages
95-
```
96-
// Welcome to create a PR to complete the code of this language, thanks!
97-
```

0 commit comments

Comments
 (0)