28. 实现strStr()

思路:

这题没啥好说的,就当练习js的语法了吧。

 

 1 var strStr = function(haystack, needle) {
 2         // return haystack.search(needle);
 3         // return haystack.indexOf(needle);
 4         if(haystack.length < needle.length){
 5             return -1;
 6         }
 7         for(let i = 0; i <= haystack.length-needle.length; i++){
 8             if(needle === haystack.substr(i, needle.length)){
 9                 return i;
10             }
11         }
12         return -1;
13     };

 

posted @ 2022-04-06 19:21  BJFU-VTH  阅读(26)  评论(0编辑  收藏  举报