leetcode_p28_strStr()——js实现

2018-07-17  本文已影响0人  kayleeWei
输入: haystack = "hello", needle = "ll"
输出: 2
输入: haystack = "aaaaa", needle = "bba"
输出: -1
/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
    if (!needle) {
        return 0
    }
    let haystackLen = haystack.length
    let needleLen = needle.length
    for(let i = 0; i < haystackLen - needleLen + 1; ++i) {
        if(haystack.substring(i, i + needleLen) == needle) {
            return i
            break
        }
    }
    return -1
};
上一篇下一篇

猜你喜欢

热点阅读