【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
};
相关推荐
涛ing7 分钟前
32. C 语言 安全函数( _s 尾缀)
linux·c语言·c++·vscode·算法·安全·vim
独正己身1 小时前
代码随想录day4
数据结构·c++·算法
利刃大大4 小时前
【回溯+剪枝】找出所有子集的异或总和再求和 && 全排列Ⅱ
c++·算法·深度优先·剪枝
皮卡丘のcoding4 小时前
蓝桥杯备赛练习题01
职场和发展·蓝桥杯
Rachela_z4 小时前
代码随想录算法训练营第十四天| 二叉树2
数据结构·算法
细嗅蔷薇@4 小时前
迪杰斯特拉(Dijkstra)算法
数据结构·算法
追求源于热爱!4 小时前
记5(一元逻辑回归+线性分类器+多元逻辑回归
算法·机器学习·逻辑回归
不过四级不改名6774 小时前
蓝桥杯嵌入式uart,iic,adc_scan模版
职场和发展·蓝桥杯
ElseWhereR4 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
Smark.5 小时前
Gurobi基础语法之 addConstr, addConstrs, addQConstr, addMQConstr
算法