给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
输入: haystack = "hello", needle = "ll" 输出: 2
输入: haystack = "aaaaa", needle = "bba" 输出: -1
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
return haystack.indexOf(needle)
};
Runtime: 52 ms, faster than 89.90% of JavaScript online submissions for Implement strStr().
Memory Usage: 33.9 MB, less than 71.32% of JavaScript online submissions for Implement strStr().
Runtime: 56 ms, faster than 79.86% of JavaScript online submissions for Implement strStr().
Memory Usage: 36 MB, less than 13.39% of JavaScript online submissions for Implement strStr().
Runtime: 56 ms, faster than 79.86% of JavaScript online submissions for Implement strStr().
Memory Usage: 34.9 MB, less than 52.69% of JavaScript online submissions for Implement strStr().