【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
};
相关推荐
小徐Chao努力15 小时前
Go语言核心知识点底层原理教程【Slice的底层实现】
开发语言·算法·golang
赫凯15 小时前
【强化学习】第三章 马尔可夫决策过程
python·算法
沐雪架构师15 小时前
大模型Agent面试精选题(第五辑)-Agent提示词工程
java·面试·职场和发展
资生算法程序员_畅想家_剑魔15 小时前
算法-动态规划-13
算法·动态规划
k***921615 小时前
list 迭代器:C++ 容器封装的 “行为统一” 艺术
java·开发语言·数据结构·c++·算法·list
natide15 小时前
词汇/表达差异-6-n-gram分布距离
人工智能·python·算法
xu_yule16 小时前
算法基础-多源最短路
c++·算法·多源最短路
火羽白麟16 小时前
大坝安全的“大脑”——模型与算法
算法·模型·大坝安全
x70x8016 小时前
C++中auto的使用
开发语言·数据结构·c++·算法·深度优先
xu_yule16 小时前
算法基础-单源最短路
c++·算法·单源最短路·bellman-ford算法·spfa算法