File tree 2 files changed +29
-1
lines changed
algorithms/implementStrstr 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 414
414
| 31| [ Next Permutation] ( https://leetcode.com/problems/next-permutation/ ) | | Medium|
415
415
| 30| [ Substring with Concatenation of All Words] ( https://leetcode.com/problems/substring-with-concatenation-of-all-words/ ) | | Hard|
416
416
| 29| [ Divide Two Integers] ( https://leetcode.com/problems/divide-two-integers/ ) | | Medium|
417
- | 28| [ Implement strStr()] ( https://leetcode.com/problems/implement-strstr/ ) | | Easy|
417
+ | 28| [ Implement strStr()] ( https://leetcode.com/problems/implement-strstr/ ) | [ js ] ( ./algorithms/implementStrstr/solution.js ) | Easy|
418
418
| 27| [ Remove Element] ( https://leetcode.com/problems/remove-element/ ) | [ js] ( ./algorithms/removeElement/removeElement.js ) [ python] ( ./algorithms/removeElement/removeElement.python ) | Easy|
419
419
| 26| [ Remove Duplicates from Sorted Array] ( https://leetcode.com/problems/remove-duplicates-from-sorted-array/ ) | [ java] ( ./algorithms/removeDuplicatesFromSortedArray/Solution.java ) , [ js] ( ./algorithms/removeDuplicatesFromSortedArray/Solution.js ) | Easy|
420
420
| 25| [ Reverse Nodes in k-Group] ( https://leetcode.com/problems/reverse-nodes-in-k-group/ ) | | Hard|
Original file line number Diff line number Diff line change
1
+ // 解法一:用 js 现成的 indexOf api
2
+ var strStr = function ( haystack , needle ) {
3
+ if ( ! needle ) return 0 ;
4
+ return haystack . indexOf ( needle )
5
+ } ;
6
+
7
+
8
+ // TODO: 解法二:自己实现
9
+ // var strStr = function(haystack, needle) {
10
+ // // 不存在或者相等时,返回0
11
+ // if (!needle || needle === haystack) return 0;
12
+ // let h = 0, n = 0, index = 0;
13
+ // while (h < haystack.length && haystack.length < needle.length) {
14
+ // while (haystack[h] === needle[n] && n < needle.length) {
15
+ // if (index === 0) {
16
+ // index = h
17
+ // }
18
+ // n = n + 1;
19
+ // }
20
+ // if (n === needle.length) {
21
+ // return index;
22
+ // } else {
23
+ // h = h + 1;
24
+ // }
25
+ // }
26
+ // // 找不到或者要查找的字符串大于自己的时候,返回-1
27
+ // return -1;
28
+ // };
You can’t perform that action at this time.
0 commit comments