LeetCode-3 无重复字符的最长子串

javascript实现版本

//双指针+滑动窗口,解决无重复最长子串问题
function legthOfLongestSubstring(s){
    let left = maxLen = 0;
    //hash记录遍历过的char的位置
    let usedChars = {};
    for(let right=0;right<s.length;right++){
        let char = s[right];
        //当前char曾经出现过的最后位置
        let usedIndex = userChars[char];
        if(typeof usedIndex != 'undefined' && left < usedIndex){
            left = usedIndex + 1;
        }
        else{
            maxLen = Math.max(maxLen, right - left + 1)
        }
        //不断加入当前char的位置,若重复,则记录的是char最后的位置
        userChars[char] = right;
    }
    return maxLen;
}
 
参考:
https://baijiahao.baidu.com/s?id=1673545577795988456&wfr=spider&for=pc
https://blog.csdn.net/qq_17550379/article/details/80547777?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
https://www.jianshu.com/p/94db35c34fc4
原文地址:https://www.cnblogs.com/mengff/p/13426536.html