【LeetCode 0003】【滑动窗口】无重复字符的最长子串

  1. Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

复制代码
**Input:** s = "abcabcbb"
**Output:** 3
**Explanation:** The answer is "abc", with the length of 3.

Example 2:

复制代码
**Input:** s = "bbbbb"
**Output:** 1
**Explanation:** The answer is "b", with the length of 1.

Example 3:

复制代码
**Input:** s = "pwwkew"
**Output:** 3
**Explanation:** The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.
JavaScript Solution
javascript 复制代码
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    let ans = 0
    if('' === s){
        return ans
    }   
    let [left,right] = [0,-1]
    // matain the mapping from character to isPresent Flag 
    let flags = {}
    while(left < s.length){
        // mark all different elements as 1s
        if(( right+1 ) < s.length && !flags[s[right+1]] ){
            flags[s[ right+1 ]] = 1
            right++
            ans = Math.max(ans,right-left+1)
        }else{
            // sliding leftmost 1s to 0s
            flags[s[left]] = 0
            left++
        }
     }
    return ans
};
相关推荐
仰泳的熊猫1 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
似水明俊德4 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
无极低码4 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发5 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
superior tigre5 小时前
22 括号生成
算法·深度优先
腾阳6 小时前
99%的人忽视了这一点:活着本身就是人生的意义,别让抑郁和内耗成为你的枷锁!
经验分享·程序人生·职场和发展·跳槽·学习方法·媒体
不吃西红柿的856 小时前
[职场] 内容运营求职简历范文 #笔记#职场发展
笔记·职场和发展·内容运营
liyang_8306 小时前
邦芒秘诀:职场高手都具备的三个特征
职场和发展
普通网友6 小时前
十大秘闻:揭秘霍兰德职业兴趣理论的未知面!
职场和发展·求职招聘·职场发展·单一职责原则