28. Implement strStr()(js)

28. Implement strStr()

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1题意:实现一个函数strStr(),接收两个参数,haystack中是否存在needle子串,存在则返回needle首字符在haystack中的索引,不存在则返回-1代码如下:
/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
    let hLen=haystack.length;
    let nLen=needle.length;
    let str=‘‘;
    for(var i=0;i<=hLen-nLen;i++){
        str=haystack.slice(i,nLen+i);
        if( str === needle) return i;
    }
    return -1
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10392851.html

时间: 2024-08-25 21:55:33

28. Implement strStr()(js)的相关文章

leetcode 之Implement strStr()(27)

字符串的匹配,返回匹配开始的位置,直接用暴力方式求解.为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可 直接停止匹配. char *strStr(char *haystack, char*needle) { char* p1; char* p2; char* p1_advance=haystack; //当字符串数量不足时,直接停止匹配 for (p2 = &needle[1]; *p2; p2++) p1_advance++; for (p1 = haystack; *p

28. Implement strStr()-leetcode-java

[原来在SAE的blog上,都转到CSDN了..] 28. Implement strStr()-leetcode-java 发表于 2016/02/06 题意 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串匹配问题,如果子串在主串中存在,则返回子串在主串中首次出现的第一个字符的位置,不

JavaScript(JS)之Javascript对象

JavaScript(JS)之Javascript对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的 <script language="javascript"> var aa=Number.MAX_VALUE; //利用数字对象获取可

百度静态资源(JS)公共库

     例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps.bdimg.com/libs/classlist/2014.01.31/classList.min.js cookies.js http://apps.bdimg.com/libs/Cookies.js/0.4.0/cookies.min.js dojo http://apps.bdimg.com/l

44. leetcode 28. Implement strStr()

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.

前台强大的图表(js)

http://www.jqplot.com/ 网址,自己下载下例子看一下就明白了. 这个是我之前做的项目中的例子(仅是部分代码): function publicMethod(){                         var plot1 = $.jqplot(chart, [currYear], {                 seriesColors: ["rgb(23, 108, 238)"],                 title: titles,     

创建HTML新元素(js)

1 <!-- 2 创建新的HTML元素 3 1.创建新的元素 4 2.创建新的节点 5 3.追加节点 6 4.向已有元素追加新的元素 7 --> 8 <html> 9 <body> 10 11 <div id="div1"> 12 <p id="p1">这是一个段落</p> 13 <p id="p2">这是另一个段落</p> 14 </div&g

[Leetcode][Python]28: Implement strStr()

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 28: Implement strStr()https://oj.leetcode.com/problems/implement-strstr/ Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of hays

leetCode 28. Implement strStr() 字符串

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac