add: 最长无重复字符的子串

This commit is contained in:
2020-05-02 16:06:08 +08:00
parent 4cb154f7c7
commit 801073e385
4 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,15 @@
/**
* @param {string} s
* @return {number}
*/
export const lengthOfLongestSubstring = function (s) {
let max = 0
for (let n = 0, len = s.length, inx = 0, set = new Set(); n < len; n++) {
if (n !== 0) set.delete(s[n - 1])
while (inx < len && !set.has(s[inx])) set.add(s[inx++])
max = Math.max(max, inx - n)
}
return max
}