add: 最长无重复字符的子串
This commit is contained in:
15
src/string/longest-substring-without-repeating-characters.js
Normal file
15
src/string/longest-substring-without-repeating-characters.js
Normal 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
|
||||
}
|
Reference in New Issue
Block a user