【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
};
相关推荐
IronMurphy8 小时前
【算法三十九】994. 腐烂的橘子
算法
迷茫的启明星9 小时前
各职业在当前发展阶段,使用AI的舒适区与盲区
大数据·人工智能·职场和发展
Ares-Wang9 小时前
算法》》旅行商问题 TSP、7座桥问题 哈密顿回路 深度优先 和 宽度优先
算法·深度优先·宽度优先
Liqiuyue9 小时前
Transformer:现代AI革命背后的核心模型
人工智能·算法·机器学习
WolfGang00732110 小时前
代码随想录算法训练营 Day34 | 动态规划 part07
算法·动态规划
Kk.080210 小时前
Linux(十一)fork实例练习、文件操作示例及相关面试题目分享
linux·运维·算法
潇冉沐晴11 小时前
2026CCCC第三次模拟赛 部分题解
算法
WolfGang00732111 小时前
代码随想录算法训练营 Day32 | 动态规划 part05
算法·动态规划
碧海银沙音频科技研究院11 小时前
1-1杰理蓝牙SOC的UI配置开发方法
人工智能·深度学习·算法