Leetcode16不含有重復字符的最長子字符串
給定一個字符串 s ,請你找出其中不含有重復字符的 最長連續(xù)子字符串 的長度。
答題:
/**
\* @param {string} s
\* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let queue = []
let maxLen = 0
for(let i=0;i<s.length;i++){
if(queue.includes(s[i])){
queue.splice(0,1+queue.indexOf(s[i]))
queue.push(s[i])
}else{
queue.push(s[i])
maxLen = Math.max(maxLen,queue.length)
}
}
return maxLen
};
實際上是考察你一個滑動窗口的問題,queue中包含了一個不包含重復字符的數(shù)組,如果有新加進來的字符在數(shù)組中有重復,則剔除queue中的該字符以及之前的字符。沒有的話就比較一下當前數(shù)組的長度和上一個len的大小,注意這里不是++哦