Skip to content

Commit 958f824

Browse files
committed
feat: add solution of Implement strStr()(028) with javascript
1 parent 6f9f6b5 commit 958f824

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
| [021][021-question] | [Merge Two Sorted Lists][021-tips] | [][021-java] | [][021-js] | [][021-kotlin] |
3737
| [026][026-question] | [Remove Duplicates from Sorted Array][026-tips] | [][026-java] | [][026-js] | [][026-kotlin] |
3838
| [027][027-question] | [Remove Element][027-tips] | [][027-java] | [][027-js] | [][027-kotlin] |
39-
| [028][028-question] | [Implement strStr()][028-tips] | [][028-java] | | [][028-kotlin] |
39+
| [028][028-question] | [Implement strStr()][028-tips] | [][028-java] | [][027-js] | [][028-kotlin] |
4040
| [035][035-question] | [Search Insert Position][035-tips] | [][035-java] | | [][035-kotlin] |
4141
| [038][038-question] | [Count and Say][038-tips] | [][038-java] | | [][038-kotlin] |
4242
| [053][053-question] | [Maximum Subarray][053-tips] | [][053-java] | | [][053-kotlin] |
@@ -358,6 +358,7 @@
358358
[021-js]: ./src/_021/Solution.js
359359
[026-js]: ./src/_026/Solution.js
360360
[027-js]: ./src/_027/Solution.js
361+
[028-js]: ./src/_028/Solution.js
361362
[226-js]: ./src/_226/Solution.js
362363
[561-js]: ./src/_561/Solution.js
363364
[643-js]: ./src/_643/Solution.js

src/_028/Solution.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function(haystack, needle) {
7+
if (haystack && needle) {
8+
var x = haystack.split(needle)
9+
if (x.length > 1) {
10+
return x[0].length
11+
} else {
12+
return -1
13+
}
14+
} else if (haystack === '' && needle !== ''){
15+
return -1
16+
} else {
17+
return 0
18+
}
19+
20+
};

tips/028/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ kotlin(200ms/87.50%):
4646
```kotlin
4747
class Solution {
4848
fun strStr(haystack: String, needle: String): Int {
49-
49+
5050
if (haystack.isEmpty()) {
5151
return if (needle.isEmpty()) haystack.length else -1
5252
}
@@ -85,7 +85,29 @@ class Solution {
8585
}
8686
}
8787
```
88+
javascript:
89+
```javascript
90+
/**
91+
* @param {string} haystack
92+
* @param {string} needle
93+
* @return {number}
94+
*/
95+
var strStr = function(haystack, needle) {
96+
if (haystack && needle) {
97+
var x = haystack.split(needle)
98+
if (x.length > 1) {
99+
return x[0].length
100+
} else {
101+
return -1
102+
}
103+
} else if (haystack === '' && needle !== ''){
104+
return -1
105+
} else {
106+
return 0
107+
}
88108

109+
};
110+
```
89111

90112
## 结语
91113

0 commit comments

Comments
 (0)