28. Implement strStr()

2017-11-04  本文已影响0人  Icytail

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里直接有方法实现了……

上一篇下一篇

猜你喜欢

热点阅读