From 29ea3ad082fa189c35b259e50ffaf66f4adfd20a Mon Sep 17 00:00:00 2001 From: aouos Date: Wed, 26 May 2021 10:00:14 +0800 Subject: [PATCH] =?UTF-8?q?0039.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index e3e79295..0eba785f 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -292,7 +292,32 @@ class Solution: ``` Go: +JavaScript +```js +var strStr = function (haystack, needle) { + if (needle === '') { + return 0; + } + let hayslen = haystack.length; + let needlen = needle.length; + + if (haystack === '' || hayslen < needlen) { + return -1; + } + + for (let i = 0; i <= hayslen - needlen; i++) { + if (haystack[i] === needle[0]) { + if (haystack.substr(i, needlen) === needle) { + return i; + } + } + if (i === hayslen - needlen) { + return -1; + } + } +}; +``` -----------------------