实现strStr()函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。

示例 1:

输入: haystack = "hello", needle = "ll"

输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"

输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的indexOf()定义相符。

/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (!needle || haystack === needle) {
return 0;
}
let h_len=haystack.length;
let n_len=needle.length;
for (let i = 0; i <= h_len - n_len; i++) {
let j = 0;
for (; j < n_len; j++) {
if (haystack[i + j] !== needle[j]) {
break;
}
}
if (j === n_len) {
return i;
}
}
return -1;
};

indexOf就不用思考了,看了别人的解答又想了好久才想出这种写法

leetcode 实现strStr()的更多相关文章

  1. Leetcode : eImplement strStr

    Leetcode : eImplement strStr 描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个 ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  4. LeetCode OJ--Implement strStr()

    http://oj.leetcode.com/problems/implement-strstr/ 判断一个串是否为另一个串的子串 比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为 ...

  5. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  6. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  7. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  8. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  9. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

随机推荐

  1. CentOS 修改IP地址为静态IP

    vi  /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes NAME=eth0 ...

  2. javascript instanceof,typeof的区别

    区分string 与 String的区别 为什么结果会是false呢? <script type="text/javascript"> var aColors = [& ...

  3. sql 存储过程返回值 变量名

    return 语句返回值,前台调用的参数名称为 @RETURN_VALUE

  4. ACCESS 组合字段 order by 出错

    ACCESS 组合字段 order by 出错 SELECT boardID,boardType,parentID,child,depth,rootID,(parentStr + ',' + boar ...

  5. nginx错误页面重定向

    一.Nginx错误页面优雅显示的原因?   当我们访问网站时,由于特殊的原因,经常会出现诸如403,404,503等错误,这极大的影响用户的访问体验,所以我们很有必要做一下错误页面的优雅显示,以提升用 ...

  6. jsp脚本语法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. centos7 配置dns服务器

    yum install bind ----------------------------------------------------------------------------------- ...

  8. python初步要点II

    [python初步要点II] 1.is & is not 操作符用于测试2个对象是否指向同一个对象,即 id(a) == id(b). 2.整形和字符串对象是不可变对象,python会高效地缓 ...

  9. HDFS设计理念

    [HDFS设计理念] 1. 读取整个数据集的时间延迟比读取第一条记录的延迟更重要. 2. HDFS以高延迟为代价,要求低时间延迟数据访问的应用,不适合在HDFS上运行. 3. namenode决定了集 ...

  10. PEAR

    简介:pear是php扩展与应用库(the php extension and application repository)的缩写.它是一个php扩展及应用的一个代码仓库. 编码规范:参考(http ...