【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
};
相关推荐
HAPPY酷18 小时前
std::pair` 与 `std::map` 基础
开发语言·c++·算法
喜欢吃燃面18 小时前
基础算法:高精度
开发语言·c++·学习·算法
努力学算法的蒟蒻18 小时前
day84(2.12)——leetcode面试经典150
算法·leetcode·面试
程序员酥皮蛋18 小时前
hot 100 第二十三题 23.反转链表
数据结构·算法·leetcode·链表
TracyCoder12319 小时前
LeetCode Hot100(51/100)——155. 最小栈
数据结构·算法·leetcode
wu_asia19 小时前
每日一练叁
算法
dalong1019 小时前
A24:圈住小猫游戏
笔记·算法·游戏·aardio
Y.O.U..19 小时前
力扣刷题-86.分隔链表
算法·leetcode·链表
智算菩萨19 小时前
上下文学习的贝叶斯推断视角:隐式梯度下降还是隐式贝叶斯?
人工智能·算法
项管芝士19 小时前
PMP项目管理:掌握减法与乘法艺术 提升项目效率
职场和发展