【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
};
相关推荐
MrZhangBaby1 天前
SQL-leetcode—3475. DNA 模式识别
数据库·sql·leetcode
_张一凡1 天前
【AIGC面试面经第四期】LLM-Qwen相关问答
面试·职场和发展·aigc
万能的小裴同学1 天前
C++ 鸭科夫手柄适配
开发语言·c++·算法
得物技术1 天前
RAG—Chunking策略实战|得物技术
数据库·人工智能·算法
gugugu.1 天前
算法:滑动窗口类型题目的总结
算法·哈希算法
大数据张老师1 天前
数据结构——直接插入排序
数据结构·算法·排序算法·1024程序员节
hoiii1871 天前
基于SVM与HOG特征的交通标志检测与识别
算法·机器学习·支持向量机
进击的炸酱面1 天前
第四章 决策树
算法·决策树·机器学习
爱coding的橙子1 天前
每日算法刷题Day81:10.29:leetcode 回溯5道题,用时2h
算法·leetcode·职场和发展
大千AI助手1 天前
Householder变换:线性代数中的镜像反射器
人工智能·线性代数·算法·决策树·机器学习·qr分解·householder算法