Skip to content

Commit 7fffbe0

Browse files
committed
feat: commit solution
1 parent 2170a1e commit 7fffbe0

File tree

6 files changed

+157
-11
lines changed

6 files changed

+157
-11
lines changed

_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
- [21.合并两个有序链表 ✅](solution/1-99/0021.merge-two-sorted-lists/)
1717
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
1818
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
19+
- [28.实现 strStr() ✅](solution/1-99/0028.implement-strstr/)
1920
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
2021
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
2122

2223

2324

2425

25-
2626

2727

index-tags.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
1818
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
1919
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
20+
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
2021

2122
#### **数组**
2223

@@ -41,6 +42,16 @@
4142

4243
#### ****
4344

45+
#### **双指针**
46+
47+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
48+
| --- | --- | --- | --- | --- |
49+
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
50+
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
51+
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
52+
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
53+
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
54+
4455
#### **链表**
4556

4657
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
@@ -57,15 +68,6 @@
5768

5869
#### **二分查找**
5970

60-
#### **双指针**
61-
62-
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
63-
| --- | --- | --- | --- | --- |
64-
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
65-
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
66-
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
67-
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
68-
6971
#### **递归**
7072

7173
#### **深度优先搜素**

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> ||
1717
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1818
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
19-
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> |
19+
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
2020
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> |
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |
2222
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [28.实现 strStr()](https://leetcode-cn.com/problems/implement-strstr)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>实现&nbsp;<a href="https://baike.baidu.com/item/strstr/811469">strStr()</a>&nbsp;函数。</p>
7+
8+
<p>给定一个&nbsp;haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回&nbsp; <strong>-1</strong>。</p>
9+
10+
<p><strong>示例 1:</strong></p>
11+
12+
<pre><strong>输入:</strong> haystack = "hello", needle = "ll"
13+
<strong>输出:</strong> 2
14+
</pre>
15+
16+
<p><strong>示例 2:</strong></p>
17+
18+
<pre><strong>输入:</strong> haystack = "aaaaa", needle = "bba"
19+
<strong>输出:</strong> -1
20+
</pre>
21+
22+
<p><strong>说明:</strong></p>
23+
24+
<p>当&nbsp;<code>needle</code>&nbsp;是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。</p>
25+
26+
<p>对于本题而言,当&nbsp;<code>needle</code>&nbsp;是空字符串时我们应当返回 0 。这与C语言的&nbsp;<a href="https://baike.baidu.com/item/strstr/811469">strstr()</a>&nbsp;以及 Java的&nbsp;<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)">indexOf()</a>&nbsp;定义相符。</p>
27+
</div>
28+
29+
30+
### 解题思路
31+
32+
![](http://lc-photo.xwlin.com/28-1.png)
33+
![](http://lc-photo.xwlin.com/28-2.png)
34+
![](http://lc-photo.xwlin.com/28-3.png)
35+
![](http://lc-photo.xwlin.com/28-4.png)
36+
37+
38+
### 代码实现
39+
40+
<!-- tabs:start -->
41+
42+
#### **Golang**
43+
```go
44+
func strStr(haystack string, needle string) int {
45+
if needle == "" {
46+
return 0
47+
}
48+
p := 0
49+
q := 0
50+
for q < len(needle) && p < len(haystack) {
51+
if haystack[p:p+1] == needle[q:q+1] {
52+
q++
53+
} else {
54+
p = p - q
55+
q = 0
56+
}
57+
p++
58+
}
59+
if q != len(needle) {
60+
return -1
61+
}
62+
return p - q
63+
}
64+
```
65+
66+
67+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=28 lang=golang
5+
*
6+
* [28] 实现 strStr()
7+
*/
8+
9+
// @lc code=start
10+
func strStr(haystack string, needle string) int {
11+
if needle == "" {
12+
return 0
13+
}
14+
p := 0
15+
q := 0
16+
for q < len(needle) && p < len(haystack) {
17+
if haystack[p:p+1] == needle[q:q+1] {
18+
q++
19+
} else {
20+
p = p - q
21+
q = 0
22+
}
23+
p++
24+
}
25+
if q != len(needle) {
26+
return -1
27+
}
28+
return p - q
29+
}
30+
31+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestStrStr(t *testing.T) {
8+
var haystack string
9+
var needle string
10+
var ret int
11+
12+
haystack = "aaa"
13+
needle = ""
14+
ret = 0
15+
if ret != strStr(haystack, needle) {
16+
t.Fatalf("case fails: %v\n", ret)
17+
}
18+
19+
haystack = "aaa"
20+
needle = "a"
21+
ret = 0
22+
if ret != strStr(haystack, needle) {
23+
t.Fatalf("case fails: %v\n", ret)
24+
}
25+
26+
haystack = "hello"
27+
needle = "ll"
28+
ret = 2
29+
if ret != strStr(haystack, needle) {
30+
t.Fatalf("case fails: %v\n", ret)
31+
}
32+
33+
haystack = "aaaaa"
34+
needle = "bba"
35+
ret = -1
36+
if ret != strStr(haystack, needle) {
37+
t.Fatalf("case fails: %v\n", ret)
38+
}
39+
40+
haystack = "mississippi"
41+
needle = "issipi"
42+
ret = -1
43+
if ret != strStr(haystack, needle) {
44+
t.Fatalf("case fails: %v\n", ret)
45+
}
46+
}

0 commit comments

Comments
 (0)