实现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. Asp.netMVC中地址后缀使用.html,jsp等404错误解决

    asp.net mvc 默认的地址路径url都是没有后缀的比如 www.a.com/aa/bb 等 如果要是www.a.com/aa/bb.html需要专门写路由. 根据我之前的经验,mvc的路由是相 ...

  2. Asp.net 中高亮显示搜索关键字简单方法

    今天用到搜索时的高亮显示,百度了一下,如下面: 1.替换关键字,对字体变色.         public static string ReplaceRed(string strtitle, stri ...

  3. C# DataTable添加行和列

    方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Colu ...

  4. PHP环境 PDOException PDOException: could not find driver

    PDOException PDOException: could not find driver in dbcon.php:29 修改php.ini文件中的相关内容.对于找不到php.ini证明你的p ...

  5. delphi使用 DockForm DesignEditors F2613 Unit 'DockForm' not found

    DockForm [dcc32 Fatal Error] ToolsAPI.pas(18): F2613 Unit 'DockForm' not found. 这样解决了XE7. http://doc ...

  6. tensorflow 卷积神经网络基本参数()

    目录: 1. tf.placeholder_with_default(tf.constant(1.0),shape=[],name='use_dropout')   # 设置一个占位符 2. tf.c ...

  7. 怎样给oracle数据库的用户解锁

    找到并进入运行窗口:(可以用windows+r快捷键哦)输入sqlplus命令:如图   进入新窗口后使用scott/tiger用户和密码进行登录会发现登录不成功:如图   这时我们可以使用syste ...

  8. cmake 强制链接静态库

    add_executable(main main.cpp) target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a) 静态库和动态库共 ...

  9. Golang作用域—坑

    先举个栗子,全局作用域变量,与 := 符号声明赋值新变量 package main import "fmt" var a = "GG" func main() ...

  10. [Linux] Big-endian and Little-endian (大小端模式)

    Big-endian Little-endian 大小端模式   https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...