【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 天前
蓝桥杯中Python常用的库与模块
python·算法
立志成为大牛的小牛1 天前
数据结构——四十四、平衡二叉树的删除操作(王道408)
数据结构·学习·程序人生·考研·算法
Suckerbin1 天前
一次LeeCode刷题记录:接雨水
算法
Blossom.1181 天前
RLHF的“炼狱“突围:从PPO到DPO的工业级对齐实战
大数据·人工智能·分布式·python·算法·机器学习·边缘计算
MobotStone1 天前
从问答到决策:Agentic AI如何重新定义AI智能体的未来
人工智能·算法
Shemol1 天前
二叉树的三种迭代遍历(无栈版本)-- 我在马克思主义课上的一些巧思
算法
胖咕噜的稞达鸭1 天前
进程状态,孤儿进程僵尸进程,Linux真实调度算法,进程切换
linux·运维·算法
RTC老炮1 天前
webrtc降噪-WienerFilter源码分析与算法原理
算法·webrtc
hweiyu001 天前
数据结构:数组
数据结构·算法
无限进步_1 天前
C语言单向链表实现详解:从基础操作到完整测试
c语言·开发语言·数据结构·c++·算法·链表·visual studio