美文网首页
28. Implement strStr()

28. Implement strStr()

作者: Icytail | 来源:发表于2017-11-04 22:32 被阅读0次

    Description:

    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
    

    My code:

    /**
     * @param {string} haystack
     * @param {string} needle
     * @return {number}
     */
    var strStr = function(haystack, needle) {
        return haystack.indexOf(needle);
    };
    

    Note: 这个在JavaScript里直接有方法实现了……

    相关文章

      网友评论

          本文标题:28. Implement strStr()

          本文链接:https://www.haomeiwen.com/subject/fxiqmxtx.html