【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
};
相关推荐
程序员-Benothing5 分钟前
数据库的脏读、不可重复读和幻读:从面试标准答案到源码级原理
数据库·面试·职场和发展
程序员小远13 分钟前
接口测试知识总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
HanhahnaH15 分钟前
各数据结构操作的时间复杂度汇总
数据结构·算法
Yzzz-F26 分钟前
CF2023D
c++·算法·dp
小小帅呀35 分钟前
学习 VLA 第6天:SWIN VIT算法原理以及复现
学习·算法
猎头南楼1 小时前
杭州算法工程师,偏大模型应用落地
算法
间歇性努力持续性发呆的野生快乐选手1 小时前
二分模板(整型,浮点型)
数据结构·c++·算法
evans在进步2 小时前
LeetCode 189 轮转数组:三次反转为什么能实现右旋?
数据结构·算法·leetcode
豆沙沙包?2 小时前
函数重载/类和对象(P14-P60)
前端·算法