You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
代码中的 res 变量存储的是所有不重复子串及其长度,就本题目完全可以不需要这个变量,如果题目需要返回不重复子串则可以用到
var lengthOfLongestSubstring = function (str) {
if (!str) return 0
let res = {} // 存储所有不重复子串
let max = 0
let s = ''
for (const char of str) {
let idx = s.indexOf(char)
s += char
if (idx === -1) { // 不重复
res[s] = s.length // 存储不重复子串
max = Math.max(max, s.length)
} else { // 重复,从重复的字符的第一个个位置进行分割
let del = s.slice(0, idx + 1)
res[del] = del.length // 存储不重复子串
max = Math.max(max, del.length)
s = s.slice(idx + 1)
res[s] = s.length // 存储不重复子串
max = Math.max(max, s.length)
}
}
res[s] = s.length
max = Math.max(max, s.length)
return max
}
var lengthOfLongestSubstring = function (s) {
let next = 0;
let start = 0;
let arr = [];
let len = 0;
while (true) {
if (arr.includes(s[next])) {
arr = [];
start++;
next = start;
if (start === s.length - 1) {
return len;
break;
}
} else {
if (s[next]) {
arr.push(s[next]);
next++;
} else {
return len;
break;
}
}
len = arr.length > len ? arr.length : len;
}
};
let lengthOfLongestSubstring(str) {
//对原数组进行判断,是否在arr里 如果在就将arr字符串之前的全部去除,不在就直接push,最后判断长度
let arr = []
let length = 0
for (let m of str) {
if (arr.includes(m)) {
let index = arr.indexOf(m)
arr.splice(0, index + 1);
}
arr.push(m);
length = Math.max(arr.length, length)
}
return length
}
Activity
[-]Leetcode3:无重复字符的最长子串[/-][+]字节&Leetcode3:无重复字符的最长子串[/+]AiKunnnnn commentedon Apr 21, 2020
对原数组进行判断,是否在arr里 如果在就将arr字符串之前的全部去除,不在就直接push,最后判断长度
Aiyoweioo commentedon Apr 21, 2020
快慢指针维护一个滑动窗口,如果滑动窗口里面有快指针fastp所指向的元素(记录这重复元素在滑动窗口的索引tmp),那么滑动窗口要缩小,即慢指针slowp要移动到tmp的后一个元素。
细节: 快指针也要从0开始,如果从1开始,类似'aaaa',结果就是0,因为tmp永远不等于-1
fanefanes commentedon Apr 21, 2020
方法一:悬浮框圈出满足条件的字符串,时间复杂度O(n),空间复杂度O(n),
方法二:双悬浮框从字符串首尾圈出满足条件的字符串,直到重合,时间复杂度O(n/2),空间复杂度O(n),
sisterAn commentedon Apr 21, 2020
解法一:维护数组
解题思路: 使用一个数组来维护滑动窗口
遍历字符串,判断字符是否在滑动窗口数组里
push进数组push进数组max更新为当前最长子串的长度遍历完,返回
max即可画图帮助理解一下:
代码实现:
时间复杂度:O(n2), 其中
arr.indexOf()时间复杂度为 O(n) ,arr.splice(0, index+1)的时间复杂度也为 O(n)空间复杂度:O(n)
解法二:维护下标
解题思路: 使用下标来维护滑动窗口
代码实现:
时间复杂度:O(n2)
空间复杂度:O(n)
解法三:优化的Map
解题思路:
使用
map来存储当前已经遍历过的字符,key为字符,value为下标使用
i来标记无重复子串开始下标,j为当前遍历字符下标遍历字符串,判断当前字符是否已经在
map中存在,存在则更新无重复子串开始下标i为相同字符的下一位置,此时从i到j为最新的无重复子串,更新max,将当前字符与下标放入map中最后,返回
max即可代码实现:
时间复杂度:O(n)
空间复杂度:O(n)
leetcode
zhaojinzhao commentedon May 7, 2020
function getMaxLen(str) {
var maxLen = 0, startIndex = 0;
for(var i = 0; i < str.length; i++) {
for(var j = startIndex; j < i; j++) {
if (str[j] === str[i]){
startIndex = j + 1;
break
}
}
maxLen = Math.max(maxLen, i - startIndex + 1);
}
return maxLen;
}
fengmiaosen commentedon Jun 16, 2020
经典的滑动窗口算法题
lovetingyuan commentedon Jul 5, 2020
U-LAN commentedon Oct 21, 2020
代码中的 res 变量存储的是所有不重复子串及其长度,就本题目完全可以不需要这个变量,如果题目需要返回不重复子串则可以用到
limeng01 commentedon Dec 7, 2020
悬浮窗
JohnApache commentedon Apr 28, 2021
利用了
Map存储已经存放的charXW666 commentedon Jun 30, 2021
AlexZhang11 commentedon May 31, 2024
function getMostNoRepeatChar(str){ let obj = {} let maxLen = 0 let curCount = 0 for (let i=0;i<str.length;i++){ if(obj.hasOwnProperty(str.charAt(i))){ maxLen = curCount>maxLen?curCount:maxLen curCount = 0 obj={} i-- }else{ obj[str.charAt(i)] = true curCount++ } } return maxLen } console.log(getMostNoRepeatChar('abcabcbb')) console.log(getMostNoRepeatChar('bbbbb')) console.log(getMostNoRepeatChar('pwwkew'))