给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
示例 4:
输入: s = ""
输出: 0
解析
第一种 枚举所有的情况
function lengthOfLongestSubstring(s){
const len = s.length
let queue = []
let max = 0
for(let i = 0; i<len; i++){
queue = []
for(let j = i; j<len; j++){
if(!queue.includes(s[j])){
queue.push(s[j])
if(queue.length>max){
max = queue.length
}
}else{
break
}
}
}
return max
}
第二种方法 是滑动窗口的方法
function lengthOfLongestSubstring(s){
const len = s.length
let queue = []
let res = []
let max = 0
let index = 0
while( index < len ){
const subIndex = queue.indexOf(s[index])
queue.push(s[index])
if(subIndex==-1){
if(queue.length>max){
max = queue.length
res = queue.slice()
}else{
queue = queue.slice(index)
}
}
}
return max
}
第三种方法 应该是散列法可是我不会 哈哈哈