const str = 'abcabcbb'
function test(str) {
//双指针 滑动窗口方法
let max = 0;
let i = 0, j = 1;
while (i < j && j < str.length) {
if (str[i] == str[j]) {//这里出了问题,只能判断一个;
max = Math.max(max, j - i)
i++;
console.log(1, i, j, max)
} else {
max = Math.max(max, j - i + 1)
j++;
console.log(2, i, j, max)
}
}
return max;
}
console.log(test1(str))
function test1(s) {
//双指针滑动窗口方法+set集合 delete从前面删
if (s.length == 0) return 0;
let max = 0, j = 0;
let set = new Set(); //记录每个科能的子串,是动态更新的
for (let i = 0; i < s.length; i++) {
if (!set.has(s[i])) {
set.add(s[i])
max = Math.max(max, set.size)
} else {
while (set.has(s[i])) {
set.delete(s[j])
j++;
}
set.add(s[i])
}
}
return max;
}
// const nums = [2, 7, 11, 15]
// const target = 9
// function test(nums, target) {
// //遍历+判断
// for (let i = 0; i < nums.length-1; i++) {
// for (let j = i+1; j < nums.length; j++) {
// if(nums[i]+nums[j]==target){
// return [i,j]
// }
// }
// }
// }
// console.log(test(nums,target))
// const arr = ["flower","flow","floight"]
// function test(arr){
// console.log(arr[0].length)
// //先判断不符合
// let flag = arr[0].split("")[0];
// for(let i=1;i<arr.length;i++){
// if(arr[i].split("")[0]!=flag){
// return ''
// }
// }
// //符合情况 取一个单词的前缀,再依次判断
// for(let i = 1;i<arr[0].length;i++){
// let temp = arr[0].substring(0,i)
// for(let j=1;j<arr.length;j++){
// if(arr[j].substring(0,i)!=temp){
// return arr[0].substring(0,i-1)
// }
// }
// }
// }
// console.log(test(arr))