【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 小时前
蓝桥杯 20541魔法科考试
java·数据结构·算法·蓝桥杯
star learning white1 小时前
xmC语言8
c语言·开发语言·算法
青小俊2 小时前
【代码随想录c++刷题】-二分查找 移除元素 有序数组的平方 - 第一章 数组 part 01
c++·算法·leetcode
ytttr8732 小时前
基于MATLAB实现晶体共晶凝固模拟
开发语言·算法·matlab
倦王3 小时前
力扣日刷251120
算法·leetcode·职场和发展
F_D_Z3 小时前
【k近邻】Kd树构造与最近邻搜索示例
算法·机器学习·近邻算法·k近邻算法
断剑zou天涯3 小时前
【算法笔记】从暴力递归到动态规划(二)
java·算法·动态规划
RTC老炮3 小时前
webrtc降噪-SpeechProbabilityEstimator类源码分析与算法原理
算法·webrtc
WWZZ20253 小时前
快速上手大模型:深度学习9(池化层、卷积神经网络1)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
Boop_wu3 小时前
[Java EE] 多线程编程初阶
java·jvm·算法